Skip to content

Commit 8af5ad5

Browse files
add generic tests for commands building
1 parent defe935 commit 8af5ad5

File tree

5 files changed

+154
-10
lines changed

5 files changed

+154
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[![codecov](https://codecov.io/gh/codefresh-io/cf-argo-plugin/branch/master/graph/badge.svg?token=DFIBSE8YTB)](https://codecov.io/gh/codefresh-io/cf-argo-plugin)

pkg/builder/builder.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,7 @@ func (b *builder) Sync(args *SyncArgs, name string, authToken string, host strin
6969
tokenFlags := buildTokenFlags(authToken, *hostDomain, args.Prune)
7070
tokenFlagsWithoutPrune := buildTokenFlags(authToken, *hostDomain, false)
7171
if args.WaitHealthy || args.WaitForSuspend {
72-
cmd := fmt.Sprintf(`
73-
cf-argo-plugin wait-rollout %s --cf-host=$CF_URL --cf-token=$CF_API_KEY --cf-integration=%s --pipeline-id=$CF_PIPELINE_NAME --build-id=$CF_BUILD_ID &
74-
sleep 5s
75-
`, name, context)
76-
b.lines = append(b.lines, cmd)
72+
b.lines = append(b.lines, GetCommandsFactory().CreateWaitRolloutCMD(name, context))
7773
}
7874

7975
if args.Sync {
@@ -113,11 +109,7 @@ func (b *builder) Rollout(args *RolloutArgs, name string, authToken string, host
113109

114110
if args.WaitHealthy {
115111
if context != "" {
116-
cmd := fmt.Sprintf(`
117-
cf-argo-plugin wait-rollout %s --cf-host=$CF_URL --cf-token=$CF_API_KEY --cf-integration=%s --pipeline-id=$CF_PIPELINE_NAME --build-id=$CF_BUILD_ID &
118-
sleep 5s
119-
`, name, context)
120-
b.lines = append(b.lines, cmd)
112+
b.lines = append(b.lines, GetCommandsFactory().CreateWaitRolloutCMD(name, context))
121113
}
122114
tokenFlags := buildTokenFlags(authToken, *hostDomain, false)
123115
b.lines = append(b.lines, fmt.Sprintf("argocd app wait %s %s %s", name, args.WaitAdditionalFlags, tokenFlags))

pkg/builder/builder_test.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package builder
22

33
import (
4+
"reflect"
45
"testing"
56
)
67

@@ -17,5 +18,122 @@ func TestBuildTokenFlagsWithoutPrune(t *testing.T) {
1718
if tokenParams != " --auth-token token --server host --insecure" {
1819
t.Errorf("Wrong \"%s\" token command ", tokenParams)
1920
}
21+
}
22+
23+
func TestRolloutWithoutWaitHealthy(t *testing.T) {
24+
builder := New()
25+
builder.Rollout(&RolloutArgs{
26+
KubernetesContext: "kube-ctx",
27+
RolloutName: "app",
28+
RolloutNamespace: "default",
29+
WaitHealthy: false,
30+
WaitAdditionalFlags: "",
31+
Debug: false,
32+
}, "test", "token", "host", "context")
33+
34+
expectedLines := []string{
35+
"#!/bin/bash -e",
36+
"kubectl config get-contexts",
37+
"kubectl config use-context \"kube-ctx\"",
38+
"kubectl argo rollouts promote \"app\" -n \"default\"",
39+
}
40+
41+
lines := builder.GetLines()
42+
43+
if !reflect.DeepEqual(expectedLines, lines) {
44+
t.Error("Rollout commands is incorrect")
45+
}
46+
47+
}
48+
49+
func TestRolloutWithWaitHealthy(t *testing.T) {
50+
builder := New()
51+
builder.Rollout(&RolloutArgs{
52+
KubernetesContext: "kube-ctx",
53+
RolloutName: "app",
54+
RolloutNamespace: "default",
55+
WaitHealthy: true,
56+
WaitAdditionalFlags: "",
57+
Debug: false,
58+
}, "test", "token", "host", "context")
59+
60+
expectedLines := []string{
61+
"#!/bin/bash -e",
62+
"kubectl config get-contexts",
63+
"kubectl config use-context \"kube-ctx\"",
64+
"kubectl argo rollouts promote \"app\" -n \"default\"",
65+
`
66+
cf-argo-plugin wait-rollout test --cf-host=$CF_URL --cf-token=$CF_API_KEY --cf-integration=context --pipeline-id=$CF_PIPELINE_NAME --build-id=$CF_BUILD_ID &
67+
sleep 5s
68+
`,
69+
"argocd app wait test --auth-token token --server --insecure",
70+
}
71+
72+
lines := builder.GetLines()
73+
74+
if !reflect.DeepEqual(expectedLines, lines) {
75+
t.Error("Rollout commands is incorrect")
76+
}
77+
78+
}
79+
80+
func TestSyncWithoutWaitHealthy(t *testing.T) {
81+
builder := New()
82+
builder.Sync(&SyncArgs{
83+
WaitHealthy: false,
84+
WaitAdditionalFlags: "",
85+
Debug: false,
86+
Sync: true,
87+
}, "test", "token", "host", "context")
88+
89+
expectedLines := []string{
90+
"#!/bin/bash -e",
91+
"argocd app sync test --auth-token token --server --insecure",
92+
}
93+
94+
lines := builder.GetLines()
95+
96+
if !reflect.DeepEqual(expectedLines, lines) {
97+
t.Error("Sync commands is incorrect")
98+
}
99+
100+
}
101+
102+
func TestSyncWithWaitHealthy(t *testing.T) {
103+
builder := New()
104+
builder.Sync(&SyncArgs{
105+
WaitHealthy: true,
106+
WaitAdditionalFlags: "",
107+
Debug: false,
108+
Sync: true,
109+
}, "test", "token", "host", "context")
110+
111+
expectedLines := []string{
112+
"#!/bin/bash -e",
113+
`
114+
cf-argo-plugin wait-rollout test --cf-host=$CF_URL --cf-token=$CF_API_KEY --cf-integration=context --pipeline-id=$CF_PIPELINE_NAME --build-id=$CF_BUILD_ID &
115+
sleep 5s
116+
`,
117+
"argocd app sync test --auth-token token --server --insecure",
118+
`
119+
{
120+
set +e
121+
argocd app wait test --auth-token token --server --insecure 2> /codefresh/volume/sync_error.log
122+
}
123+
if [[ $? -ne 0 ]]; then
124+
ARGO_SYNC_ERROR=$(cat /codefresh/volume/sync_error.log | grep -i fatal)
125+
fi
126+
echo ARGO_SYNC_ERROR="$ARGO_SYNC_ERROR"
127+
cf_export ARGO_SYNC_ERROR="$ARGO_SYNC_ERROR"
128+
129+
wait
130+
`,
131+
}
132+
133+
lines := builder.GetLines()
134+
135+
if !reflect.DeepEqual(expectedLines, lines) {
136+
t.Error("Sync commands is incorrect")
137+
}
20138

21139
}

pkg/builder/commands_factory.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package builder
2+
3+
import "fmt"
4+
5+
type CommandsFactory struct {
6+
}
7+
8+
func GetCommandsFactory() *CommandsFactory {
9+
return &CommandsFactory{}
10+
}
11+
12+
func (cf *CommandsFactory) CreateWaitRolloutCMD(name string, integration string) string {
13+
return fmt.Sprintf(`
14+
cf-argo-plugin wait-rollout %s --cf-host=$CF_URL --cf-token=$CF_API_KEY --cf-integration=%s --pipeline-id=$CF_PIPELINE_NAME --build-id=$CF_BUILD_ID &
15+
sleep 5s
16+
`, name, integration)
17+
}

pkg/builder/commands_factory_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package builder
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestCreateWaitRolloutCMD(t *testing.T) {
8+
expectedCMD := `
9+
cf-argo-plugin wait-rollout test --cf-host=$CF_URL --cf-token=$CF_API_KEY --cf-integration=context --pipeline-id=$CF_PIPELINE_NAME --build-id=$CF_BUILD_ID &
10+
sleep 5s
11+
`
12+
cmd := GetCommandsFactory().CreateWaitRolloutCMD("test", "context")
13+
if cmd != expectedCMD {
14+
t.Error("Wait rollout cmd is wrong")
15+
}
16+
}

0 commit comments

Comments
 (0)