Skip to content

Commit

Permalink
feat: e2e for clusters (argoproj#7118)
Browse files Browse the repository at this point in the history
feat: e2e for clusters (argoproj#7118)

Signed-off-by: pashavictorovich <[email protected]>
  • Loading branch information
pasha-codefresh authored Aug 31, 2021
1 parent ce1d803 commit a9f009c
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 9 deletions.
4 changes: 2 additions & 2 deletions cmd/argocd/commands/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,13 @@ func NewClusterRemoveCommand(clientOpts *argocdclient.ClientOptions) *cobra.Comm
// Print table of cluster information
func printClusterTable(clusters []argoappv1.Cluster) {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
_, _ = fmt.Fprintf(w, "SERVER\tNAME\tVERSION\tSTATUS\tMESSAGE\n")
_, _ = fmt.Fprintf(w, "SERVER\tNAME\tVERSION\tSTATUS\tMESSAGE\tPROJECT\n")
for _, c := range clusters {
server := c.Server
if len(c.Namespaces) > 0 {
server = fmt.Sprintf("%s (%d namespaces)", c.Server, len(c.Namespaces))
}
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", server, c.Name, c.ServerVersion, c.ConnectionState.Status, c.ConnectionState.Message)
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\n", server, c.Name, c.ServerVersion, c.ConnectionState.Status, c.ConnectionState.Message, c.Project)
}
_ = w.Flush()
}
Expand Down
43 changes: 36 additions & 7 deletions test/e2e/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,56 @@ import (

"github.com/stretchr/testify/assert"

. "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
. "github.com/argoproj/argo-cd/v2/test/e2e/fixture"
clusterFixture "github.com/argoproj/argo-cd/v2/test/e2e/fixture/cluster"
. "github.com/argoproj/argo-cd/v2/util/errors"
)

func TestClusterList(t *testing.T) {
SkipIfAlreadyRun(t)
defer RecordTestRun(t)
output := FailOnErr(RunCli("cluster", "list")).(string)
assert.Equal(t, fmt.Sprintf(`SERVER NAME VERSION STATUS MESSAGE
https://kubernetes.default.svc in-cluster %v Successful `, GetVersions().ServerVersion), output)

clusterFixture.
Given(t).
Project(ProjectName).
When().
List().
Then().
AndCLIOutput(func(output string, err error) {
assert.Equal(t, fmt.Sprintf(`SERVER NAME VERSION STATUS MESSAGE PROJECT
https://kubernetes.default.svc in-cluster %v Successful `, GetVersions().ServerVersion), output)
})
}

func TestClusterAdd(t *testing.T) {
SkipIfAlreadyRun(t)
defer RecordTestRun(t)

clusterFixture.
Given(t).
Project(ProjectName).
Upsert(true).
Server(KubernetesInternalAPIServerAddr).
When().
Create().
List().
Then().
AndCLIOutput(func(output string, err error) {
assert.Equal(t, fmt.Sprintf(`SERVER NAME VERSION STATUS MESSAGE PROJECT
https://kubernetes.default.svc test-cluster-add %v Successful %s`, GetVersions().ServerVersion, ProjectName), output)
})
}

func TestClusterGet(t *testing.T) {
SkipIfAlreadyRun(t)
defer RecordTestRun(t)
output := FailOnErr(RunCli("cluster", "get", "https://kubernetes.default.svc")).(string)

assert.Contains(t, output, fmt.Sprintf(`
name: in-cluster
server: https://kubernetes.default.svc
serverVersion: "%v"`, GetVersions().ServerVersion))
assert.Contains(t, output, "name: test-cluster-add")
assert.Contains(t, output, "server: https://kubernetes.default.svc")
assert.Contains(t, output, fmt.Sprintf(`project: %s`, ProjectName))
assert.Contains(t, output, fmt.Sprintf(`serverVersion: "%v"`, GetVersions().ServerVersion))

assert.Contains(t, output, `config:
tlsClientConfig:
Expand Down
81 changes: 81 additions & 0 deletions test/e2e/fixture/cluster/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package cluster

import (
"context"
"log"

"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"

clusterpkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/cluster"
"github.com/argoproj/argo-cd/v2/test/e2e/fixture"
)

// this implements the "when" part of given/when/then
//
// none of the func implement error checks, and that is complete intended, you should check for errors
// using the Then()
type Actions struct {
context *Context
lastOutput string
lastError error
ignoreErrors bool
}

func (a *Actions) IgnoreErrors() *Actions {
a.ignoreErrors = true
return a
}

func (a *Actions) DoNotIgnoreErrors() *Actions {
a.ignoreErrors = false
return a
}

func (a *Actions) Create(args ...string) *Actions {
_, clusterClient, _ := fixture.ArgoCDClientset.NewClusterClient()

_, err := clusterClient.Create(context.Background(), &clusterpkg.ClusterCreateRequest{
Cluster: &v1alpha1.Cluster{
Server: a.context.server,
Name: a.context.name,
Config: v1alpha1.ClusterConfig{},
ConnectionState: v1alpha1.ConnectionState{},
ServerVersion: "",
Namespaces: nil,
RefreshRequestedAt: nil,
Info: v1alpha1.ClusterInfo{},
Shard: nil,
ClusterResources: false,
Project: a.context.project,
},
Upsert: a.context.upsert,
})

if err != nil {
log.Fatalf("Failed to upser cluster %v", err.Error())
}

return a
}

func (a *Actions) List() *Actions {
a.context.t.Helper()
a.runCli("cluster", "list")
return a
}

func (a *Actions) Get() *Actions {
a.context.t.Helper()
a.runCli("cluster", "get", a.context.server)
return a
}

func (a *Actions) Then() *Consequences {
a.context.t.Helper()
return &Consequences{a.context, a}
}

func (a *Actions) runCli(args ...string) {
a.context.t.Helper()
a.lastOutput, a.lastError = fixture.RunCli(args...)
}
58 changes: 58 additions & 0 deletions test/e2e/fixture/cluster/consequences.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cluster

import (
"context"
"fmt"

clusterpkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/cluster"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/v2/test/e2e/fixture"
)

// this implements the "then" part of given/when/then
type Consequences struct {
context *Context
actions *Actions
}

func (c *Consequences) Expect() *Consequences {
return c
}

func (c *Consequences) And(block func(cluster *v1alpha1.Cluster, err error)) *Consequences {
c.context.t.Helper()
block(c.cluster())
return c
}

func (c *Consequences) AndCLIOutput(block func(output string, err error)) *Consequences {
c.context.t.Helper()
block(c.actions.lastOutput, c.actions.lastError)
return c
}

func (c *Consequences) cluster() (*v1alpha1.Cluster, error) {
app, err := c.get()
return app, err
}

func (c *Consequences) get() (*v1alpha1.Cluster, error) {
_, clusterClient, _ := fixture.ArgoCDClientset.NewClusterClient()

cluster, _ := clusterClient.List(context.Background(), &clusterpkg.ClusterQuery{})
for i := range cluster.Items {
if cluster.Items[i].Server == c.context.server {
return &cluster.Items[i], nil
}
}

return nil, fmt.Errorf("cluster not found")
}

func (c *Consequences) Given() *Context {
return c.context
}

func (c *Consequences) When() *Actions {
return c.actions
}
67 changes: 67 additions & 0 deletions test/e2e/fixture/cluster/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package cluster

import (
"testing"
"time"

"github.com/argoproj/argo-cd/v2/test/e2e/fixture"
"github.com/argoproj/argo-cd/v2/util/env"
)

// this implements the "given" part of given/when/then
type Context struct {
t *testing.T
// seconds
timeout int
name string
project string
server string
upsert bool
}

func Given(t *testing.T) *Context {
fixture.EnsureCleanState(t)
return GivenWithSameState(t)
}

func GivenWithSameState(t *testing.T) *Context {
// ARGOCE_E2E_DEFAULT_TIMEOUT can be used to override the default timeout
// for any context.
timeout := env.ParseNumFromEnv("ARGOCD_E2E_DEFAULT_TIMEOUT", 10, 0, 180)
return &Context{t: t, name: fixture.Name(), timeout: timeout, project: "default"}
}

func (c *Context) GetName() string {
return c.name
}

func (c *Context) Name(name string) *Context {
c.name = name
return c
}

func (c *Context) Server(server string) *Context {
c.server = server
return c
}

func (c *Context) And(block func()) *Context {
block()
return c
}

func (c *Context) When() *Actions {
// in case any settings have changed, pause for 1s, not great, but fine
time.Sleep(1 * time.Second)
return &Actions{context: c}
}

func (c *Context) Project(project string) *Context {
c.project = project
return c
}

func (c *Context) Upsert(upsert bool) *Context {
c.upsert = upsert
return c
}
5 changes: 5 additions & 0 deletions test/e2e/fixture/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ const (
submoduleParentDir = "submoduleParent.git"

GuestbookPath = "guestbook"

ProjectName = "argo-project"
)

const (
Expand Down Expand Up @@ -437,6 +439,9 @@ func EnsureCleanState(t *testing.T) {
// kubectl delete secrets -l argocd.argoproj.io/secret-type=repo-creds
CheckError(KubeClientset.CoreV1().Secrets(TestNamespace()).DeleteCollection(context.Background(),
v1.DeleteOptions{PropagationPolicy: &policy}, v1.ListOptions{LabelSelector: common.LabelKeySecretType + "=" + common.LabelValueSecretTypeRepoCreds}))
// kubectl delete secrets -l argocd.argoproj.io/secret-type=cluster
CheckError(KubeClientset.CoreV1().Secrets(TestNamespace()).DeleteCollection(context.Background(),
v1.DeleteOptions{PropagationPolicy: &policy}, v1.ListOptions{LabelSelector: common.LabelKeySecretType + "=" + common.LabelValueSecretTypeCluster}))
// kubectl delete secrets -l e2e.argoproj.io=true
CheckError(KubeClientset.CoreV1().Secrets(TestNamespace()).DeleteCollection(context.Background(),
v1.DeleteOptions{PropagationPolicy: &policy}, v1.ListOptions{LabelSelector: testingLabel + "=true"}))
Expand Down

0 comments on commit a9f009c

Please sign in to comment.