-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add validate logic for plugin (#7)
* feat: add validate logic for kubernetes flags * feat: add validate logic for config flags
- Loading branch information
Showing
7 changed files
with
361 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) 2020 Target Brands, Inc. All rights reserved. | ||
// | ||
// Use of this source code is governed by the LICENSE file in this repository. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// Config represents the plugin configuration for Kubernetes config information. | ||
type Config struct { | ||
// Kubernetes files or directories to apply | ||
Files []string | ||
// new container images from files to apply | ||
Images []string | ||
// Kubernetes resources to watch status of | ||
Statuses []string | ||
// total time allowed to watch Kubernetes resources | ||
Timeout time.Duration | ||
} | ||
|
||
// Validate verifies the Config is properly configured. | ||
func (c *Config) Validate() error { | ||
logrus.Trace("validating config configuration") | ||
|
||
// verify files are provided | ||
if len(c.Files) == 0 { | ||
return fmt.Errorf("no config files provided") | ||
} | ||
|
||
// verify images are provided | ||
if len(c.Images) == 0 { | ||
return fmt.Errorf("no config images provided") | ||
} | ||
|
||
// verify statuses are provided | ||
if len(c.Statuses) == 0 { | ||
return fmt.Errorf("no config statuses provided") | ||
} | ||
|
||
// verify timeout is provided | ||
if c.Timeout <= 0 { | ||
return fmt.Errorf("no config timeout provided") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright (c) 2020 Target Brands, Inc. All rights reserved. | ||
// | ||
// Use of this source code is governed by the LICENSE file in this repository. | ||
|
||
package main | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestKubernetes_Config_Validate(t *testing.T) { | ||
// setup types | ||
c := &Config{ | ||
Files: []string{"files"}, | ||
Images: []string{"images"}, | ||
Statuses: []string{"statuses"}, | ||
Timeout: 5 * time.Minute, | ||
} | ||
|
||
err := c.Validate() | ||
if err != nil { | ||
t.Errorf("Validate returned err: %v", err) | ||
} | ||
} | ||
|
||
func TestKubernetes_Config_Validate_NoFiles(t *testing.T) { | ||
// setup types | ||
c := &Config{ | ||
Images: []string{"images"}, | ||
Statuses: []string{"statuses"}, | ||
Timeout: 5 * time.Minute, | ||
} | ||
|
||
err := c.Validate() | ||
if err == nil { | ||
t.Errorf("Validate should have returned err") | ||
} | ||
} | ||
|
||
func TestKubernetes_Config_Validate_NoImages(t *testing.T) { | ||
// setup types | ||
c := &Config{ | ||
Files: []string{"files"}, | ||
Statuses: []string{"statuses"}, | ||
Timeout: 5 * time.Minute, | ||
} | ||
|
||
err := c.Validate() | ||
if err == nil { | ||
t.Errorf("Validate should have returned err") | ||
} | ||
} | ||
|
||
func TestKubernetes_Config_Validate_NoStatuses(t *testing.T) { | ||
// setup types | ||
c := &Config{ | ||
Files: []string{"files"}, | ||
Images: []string{"images"}, | ||
Timeout: 5 * time.Minute, | ||
} | ||
|
||
err := c.Validate() | ||
if err == nil { | ||
t.Errorf("Validate should have returned err") | ||
} | ||
} | ||
|
||
func TestKubernetes_Config_Validate_NoTimeout(t *testing.T) { | ||
// setup types | ||
c := &Config{ | ||
Files: []string{"files"}, | ||
Images: []string{"images"}, | ||
Statuses: []string{"statuses"}, | ||
} | ||
|
||
err := c.Validate() | ||
if err == nil { | ||
t.Errorf("Validate should have returned err") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) 2020 Target Brands, Inc. All rights reserved. | ||
// | ||
// Use of this source code is governed by the LICENSE file in this repository. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// Kubernetes represents the plugin configuration for Kubernetes information. | ||
type Kubernetes struct { | ||
// cluster configuration to use for interactions | ||
Config string | ||
// cluster context to use for interactions | ||
Context string | ||
// cluster namespace to use for interactions | ||
Namespace string | ||
} | ||
|
||
// Validate verifies the Kubernetes is properly configured. | ||
func (k *Kubernetes) Validate() error { | ||
logrus.Trace("validating kubernetes configuration") | ||
|
||
// verify config is provided | ||
if len(k.Config) == 0 { | ||
return fmt.Errorf("no kubernetes config provided") | ||
} | ||
|
||
// verify context is provided | ||
if len(k.Context) == 0 { | ||
return fmt.Errorf("no kubernetes context provided") | ||
} | ||
|
||
// verify namespace is provided | ||
if len(k.Namespace) == 0 { | ||
return fmt.Errorf("no kubernetes namespace provided") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) 2020 Target Brands, Inc. All rights reserved. | ||
// | ||
// Use of this source code is governed by the LICENSE file in this repository. | ||
|
||
package main | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestKubernetes_Kubernetes_Validate(t *testing.T) { | ||
// setup types | ||
k := &Kubernetes{ | ||
Config: "config", | ||
Context: "context", | ||
Namespace: "namespace", | ||
} | ||
|
||
err := k.Validate() | ||
if err != nil { | ||
t.Errorf("Validate returned err: %v", err) | ||
} | ||
} | ||
|
||
func TestKubernetes_Kubernetes_Validate_NoConfig(t *testing.T) { | ||
// setup types | ||
k := &Kubernetes{ | ||
Context: "context", | ||
Namespace: "namespace", | ||
} | ||
|
||
err := k.Validate() | ||
if err == nil { | ||
t.Errorf("Validate should have returned err") | ||
} | ||
} | ||
|
||
func TestKubernetes_Kubernetes_Validate_NoContext(t *testing.T) { | ||
// setup types | ||
k := &Kubernetes{ | ||
Config: "config", | ||
Namespace: "namespace", | ||
} | ||
|
||
err := k.Validate() | ||
if err == nil { | ||
t.Errorf("Validate should have returned err") | ||
} | ||
} | ||
|
||
func TestKubernetes_Kubernetes_Validate_NoNamespace(t *testing.T) { | ||
// setup types | ||
k := &Kubernetes{ | ||
Config: "config", | ||
Context: "context", | ||
} | ||
|
||
err := k.Validate() | ||
if err == nil { | ||
t.Errorf("Validate should have returned err") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.