Skip to content

Commit

Permalink
refactor: made code more testable
Browse files Browse the repository at this point in the history
  • Loading branch information
alexec committed Jun 5, 2021
1 parent dd70bab commit 009731a
Show file tree
Hide file tree
Showing 22 changed files with 285 additions and 98 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## v0.0.35 (2021-06-04)

* [421c142](https://github.com/argoproj/argo-workflows/commit/421c14269c618eb1a1c601715e6ff361d45d9921) fix: total/error calcs
* [06fc5f0](https://github.com/argoproj/argo-workflows/commit/06fc5f0592ce74982864f133af5dd405948451f0) fix: only return pending for replica=0

### Contributors

* Alex Collins

## v0.0.34 (2021-06-04)

* [c73ce5b](https://github.com/argoproj/argo-workflows/commit/c73ce5b33a6bfc42abf772d6f794c6af885499a0) fix: change from patch to update for the controller
Expand Down
16 changes: 5 additions & 11 deletions api/v1alpha1/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@ import (

type Cat struct{}

func (m *Cat) getContainer(req getContainerReq) corev1.Container {
return corev1.Container{
Name: CtrMain,
Image: req.runnerImage,
ImagePullPolicy: req.imagePullPolicy,
Args: []string{"cat"},
Env: req.env,
VolumeMounts: []corev1.VolumeMount{req.volumeMount},
Resources: SmallResourceRequirements,
Lifecycle: req.lifecycle,
}
func (m Cat) getContainer(req getContainerReq) corev1.Container {
return containerBuilder{}.
init(req).
args("cat").
build()
}
13 changes: 13 additions & 0 deletions api/v1alpha1/cat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package v1alpha1

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCat_getContainer(t *testing.T) {
x := Cat{}
c := x.getContainer(getContainerReq{})
assert.Equal(t, []string{"cat"}, c.Args)
}
23 changes: 10 additions & 13 deletions api/v1alpha1/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,18 @@ type Container struct {
Env []corev1.EnvVar `json:"env,omitempty" protobuf:"bytes,8,rep,name=env"`
}

func (in *Container) getContainer(req getContainerReq) corev1.Container {
return corev1.Container{
Name: CtrMain,
Image: in.Image,
ImagePullPolicy: req.imagePullPolicy,
Command: in.Command,
Args: in.Args,
Env: append(in.Env, req.env...),
VolumeMounts: append(in.VolumeMounts, req.volumeMount),
Resources: SmallResourceRequirements,
Lifecycle: req.lifecycle,
}
func (in Container) getContainer(req getContainerReq) corev1.Container {
return containerBuilder{}.
init(req).
image(in.Image).
command(in.Command...).
args(in.Args...).
appendEnv(in.Env...).
appendVolumeMounts(in.VolumeMounts...).
build()
}

func (in *Container) GetIn() *Interface {
func (in Container) GetIn() *Interface {
if in.In != nil {
return in.In
}
Expand Down
56 changes: 56 additions & 0 deletions api/v1alpha1/container_builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package v1alpha1

import corev1 "k8s.io/api/core/v1"

// +kubebuilder:skipversion
type containerBuilder corev1.Container

func (b containerBuilder) init(req getContainerReq) containerBuilder {
b.Name = CtrMain
b.Image = req.runnerImage
b.ImagePullPolicy = req.imagePullPolicy
b.Env = req.env
b.VolumeMounts = []corev1.VolumeMount{req.volumeMount}
b.Resources = SmallResourceRequirements
b.Lifecycle = req.lifecycle
return b
}

func (b containerBuilder) args(args ...string) containerBuilder {
b.Args = args
return b
}

func (b containerBuilder) image(x string) containerBuilder {
b.Image = x
return b
}

func (b containerBuilder) resources(x corev1.ResourceRequirements) containerBuilder {
b.Resources = x
return b
}

func (b containerBuilder) command(x ...string) containerBuilder {
b.Command = x
return b
}

func (b containerBuilder) appendEnv(x ...corev1.EnvVar) containerBuilder {
b.Env = append(b.Env, x...)
return b
}

func (b containerBuilder) workingDir(x string) containerBuilder {
b.WorkingDir = x
return b
}

func (b containerBuilder) appendVolumeMounts(x ...corev1.VolumeMount) containerBuilder {
b.VolumeMounts = append(b.VolumeMounts, x...)
return b
}

func (b containerBuilder) build() corev1.Container {
return corev1.Container(b)
}
15 changes: 15 additions & 0 deletions api/v1alpha1/container_builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package v1alpha1

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_containerBuilder(t *testing.T) {
c := containerBuilder{}.
init(getContainerReq{}).
build()
assert.Equal(t, "main", c.Name)
assert.Equal(t, SmallResourceRequirements, c.Resources)
}
24 changes: 24 additions & 0 deletions api/v1alpha1/container_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package v1alpha1

import (
"testing"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
)

func TestContainer_getContainer(t *testing.T) {
x := Container{
Image: "my-image",
VolumeMounts: []corev1.VolumeMount{{Name: "my-vm"}},
Command: []string{"my-cmd"},
Args: []string{"my-args"},
Env: []corev1.EnvVar{{Name: "my-envvar"}},
}
c := x.getContainer(getContainerReq{})
assert.Equal(t, x.Image, c.Image)
assert.Contains(t, c.VolumeMounts, c.VolumeMounts[0])
assert.Equal(t, x.Command, c.Command)
assert.Equal(t, x.Args, c.Args)
assert.Equal(t, x.Env, c.Env)
}
16 changes: 5 additions & 11 deletions api/v1alpha1/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@ import (

type Expand struct{}

func (m *Expand) getContainer(req getContainerReq) corev1.Container {
return corev1.Container{
Name: CtrMain,
Image: req.runnerImage,
ImagePullPolicy: req.imagePullPolicy,
Args: []string{"expand"},
Env: req.env,
VolumeMounts: []corev1.VolumeMount{req.volumeMount},
Resources: SmallResourceRequirements,
Lifecycle: req.lifecycle,
}
func (m Expand) getContainer(req getContainerReq) corev1.Container {
return containerBuilder{}.
init(req).
args("expand").
build()
}
13 changes: 13 additions & 0 deletions api/v1alpha1/expand_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package v1alpha1

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestExpand_getContainer(t *testing.T) {
x := Expand{}
c := x.getContainer(getContainerReq{})
assert.Equal(t, []string{"expand"}, c.Args)
}
14 changes: 4 additions & 10 deletions api/v1alpha1/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@ import corev1 "k8s.io/api/core/v1"
type Filter string

func (m Filter) getContainer(req getContainerReq) corev1.Container {
return corev1.Container{
Name: CtrMain,
Image: req.runnerImage,
ImagePullPolicy: req.imagePullPolicy,
Args: []string{"filter", string(m)},
Env: req.env,
VolumeMounts: []corev1.VolumeMount{req.volumeMount},
Resources: SmallResourceRequirements,
Lifecycle: req.lifecycle,
}
return containerBuilder{}.
init(req).
args("filter", string(m)).
build()
}
13 changes: 13 additions & 0 deletions api/v1alpha1/filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package v1alpha1

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFilter_getContainer(t *testing.T) {
const x Filter = "my-filter"
c := x.getContainer(getContainerReq{})
assert.Equal(t, []string{"filter", "my-filter"}, c.Args)
}
14 changes: 4 additions & 10 deletions api/v1alpha1/flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@ import (
type Flatten struct{}

func (m *Flatten) getContainer(req getContainerReq) corev1.Container {
return corev1.Container{
Name: CtrMain,
Image: req.runnerImage,
ImagePullPolicy: req.imagePullPolicy,
Args: []string{"flatten"},
Env: req.env,
VolumeMounts: []corev1.VolumeMount{req.volumeMount},
Resources: SmallResourceRequirements,
Lifecycle: req.lifecycle,
}
return containerBuilder{}.
init(req).
args("flatten").
build()
}
13 changes: 13 additions & 0 deletions api/v1alpha1/flatten_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package v1alpha1

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFlatten_getContainer(t *testing.T) {
x := Flatten{}
c := x.getContainer(getContainerReq{})
assert.Equal(t, []string{"flatten"}, c.Args)
}
21 changes: 9 additions & 12 deletions api/v1alpha1/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ type Git struct {
Env []corev1.EnvVar `json:"env,omitempty" protobuf:"bytes,5,rep,name=env"`
}

func (in *Git) getContainer(req getContainerReq) corev1.Container {
return corev1.Container{
Name: CtrMain,
Image: in.Image,
ImagePullPolicy: req.imagePullPolicy,
Command: in.Command,
Env: append(in.Env, req.env...),
WorkingDir: PathWorkingDir,
VolumeMounts: []corev1.VolumeMount{req.volumeMount},
Resources: LargeResourceRequirements,
Lifecycle: req.lifecycle,
}
func (in Git) getContainer(req getContainerReq) corev1.Container {
return containerBuilder{}.
init(req).
image(in.Image).
command(in.Command...).
appendEnv(in.Env...).
workingDir(PathWorkingDir).
resources(LargeResourceRequirements).
build()
}
23 changes: 23 additions & 0 deletions api/v1alpha1/git_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package v1alpha1

import (
"testing"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
)

func TestGit_getContainer(t *testing.T) {
x := Git{
Image: "my-image",
Command: []string{"my-command"},
Env: []corev1.EnvVar{{Name: "my-env"}},
}
c := x.getContainer(getContainerReq{})

assert.Equal(t, x.Image, c.Image)
assert.Equal(t, x.Command, c.Command)
assert.Equal(t, x.Env, c.Env)
assert.Equal(t, PathWorkingDir, c.WorkingDir)
assert.Equal(t, LargeResourceRequirements, c.Resources)
}
15 changes: 5 additions & 10 deletions api/v1alpha1/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,11 @@ type Group struct {
}

func (g *Group) getContainer(req getContainerReq) corev1.Container {
return corev1.Container{
Name: CtrMain,
Image: req.runnerImage,
ImagePullPolicy: req.imagePullPolicy,
Args: []string{"group", g.Key, g.EndOfGroup, string(g.Format)},
Env: req.env,
VolumeMounts: []corev1.VolumeMount{g.getVolumeMount(req.volumeMount)},
Resources: SmallResourceRequirements,
Lifecycle: req.lifecycle,
}
return containerBuilder{}.
init(req).
args("group", g.Key, g.EndOfGroup, string(g.Format)).
appendVolumeMounts(g.getVolumeMount(req.volumeMount)).
build()
}

func (g *Group) getVolumeMount(defaultVolumeMount corev1.VolumeMount) corev1.VolumeMount {
Expand Down
23 changes: 23 additions & 0 deletions api/v1alpha1/group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package v1alpha1

import (
"testing"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
)

func TestGroup_getContainer(t *testing.T) {
x := Group{
Key: "my-key",
EndOfGroup: "my-eog",
Format: "my-fmt",
Storage: &Storage{
Name: "my-storage",
SubPath: "my-sub-path",
},
}
c := x.getContainer(getContainerReq{})
assert.Equal(t, []string{"group", "my-key", "my-eog", "my-fmt"}, c.Args)
assert.Contains(t, c.VolumeMounts, corev1.VolumeMount{Name: "my-storage", MountPath: "/var/run/argo-dataflow/groups", SubPath: "my-sub-path"})
}
16 changes: 6 additions & 10 deletions api/v1alpha1/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ type Handler struct {
Code string `json:"code" protobuf:"bytes,3,opt,name=code"`
}

func (in *Handler) getContainer(req getContainerReq) corev1.Container {
return corev1.Container{
Name: CtrMain,
Image: fmt.Sprintf(req.imageFormat, "dataflow-"+in.Runtime),
ImagePullPolicy: req.imagePullPolicy,
Env: req.env,
VolumeMounts: []corev1.VolumeMount{req.volumeMount},
Resources: LargeResourceRequirements,
Lifecycle: req.lifecycle,
}
func (in Handler) getContainer(req getContainerReq) corev1.Container {
return containerBuilder{}.
init(req).
image(fmt.Sprintf(req.imageFormat, "dataflow-"+in.Runtime)).
resources(LargeResourceRequirements).
build()
}
Loading

0 comments on commit 009731a

Please sign in to comment.