Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move Container to separate file #19

Merged
merged 1 commit into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions cmd/vela-kubernetes/container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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"
)

// Container represents the plugin configuration for patching a container.
//
// TODO: Enable patching of other container attributes?
// * environment variables
// * image pull policy
// * ports
// * resources
//
// This is possible if we made Container a map[string]string.
type Container struct {
// name of the container to patch
Name string
// image of the container to patch
Image string
}

// Validate verifies the Container is properly configured.
func (c *Container) Validate() error {
logrus.Tracef("validating container configuration for %s", c.Name)

// verify name is provided
if len(c.Name) == 0 {
return fmt.Errorf("no container name provided")
}

// verify image is provided
if len(c.Image) == 0 {
return fmt.Errorf("no container image provided")
}

return nil
}
46 changes: 46 additions & 0 deletions cmd/vela-kubernetes/container_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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_Container_Validate(t *testing.T) {
// setup types
c := &Container{
Name: "container",
Image: "alpine",
}

err := c.Validate()
if err != nil {
t.Errorf("Validate returned err: %v", err)
}
}

func TestKubernetes_Container_Validate_NoName(t *testing.T) {
// setup types
c := &Container{
Image: "alpine",
}

err := c.Validate()
if err == nil {
t.Errorf("Validate should have returned err")
}
}

func TestKubernetes_Container_Validate_NoImage(t *testing.T) {
// setup types
c := &Container{
Name: "container",
}

err := c.Validate()
if err == nil {
t.Errorf("Validate should have returned err")
}
}
33 changes: 0 additions & 33 deletions cmd/vela-kubernetes/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,39 +62,6 @@ spec:
commit_sha:
`

// Container represents the plugin configuration for patching a container.
//
// TODO: Enable patching of other container attributes?
// * environment variables
// * image pull policy
// * ports
// * resources
//
// This is possible if we made Container a map[string]string
type Container struct {
// name of the container to patch
Name string
// image of the container to patch
Image string
}

// Validate verifies the Container is properly configured.
func (c *Container) Validate() error {
logrus.Tracef("validating container configuration for %s", c.Name)

// verify name is provided
if len(c.Name) == 0 {
return fmt.Errorf("no container name provided")
}

// verify image is provided
if len(c.Image) == 0 {
return fmt.Errorf("no container image provided")
}

return nil
}

// Patch represents the plugin configuration for Patch config information.
type Patch struct {
// container images from files to patch
Expand Down
17 changes: 2 additions & 15 deletions cmd/vela-kubernetes/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestKubernetes_Patch_Validate_Invalid(t *testing.T) {
}
}

func TestKubernetes_Patch_Validate_NoContainers(t *testing.T) {
func TestKubernetes_Patch_Validate_NoRawContainers(t *testing.T) {
// setup types
p := &Patch{}

Expand All @@ -108,7 +108,7 @@ func TestKubernetes_Patch_Validate_NoContainers(t *testing.T) {
}
}

func TestKubernetes_Patch_Validate_NoContainerName(t *testing.T) {
func TestKubernetes_Patch_Validate_NoRawContainerName(t *testing.T) {
// setup types
p := &Patch{
Output: "json",
Expand All @@ -120,16 +120,3 @@ func TestKubernetes_Patch_Validate_NoContainerName(t *testing.T) {
t.Errorf("Validate should have returned err")
}
}

func TestKubernetes_Patch_Validate_NoContainerImage(t *testing.T) {
// setup types
p := &Patch{
Output: "json",
RawContainers: `[{"name": "container"}]`,
}

err := p.Validate()
if err == nil {
t.Errorf("Validate should have returned err")
}
}