forked from argoproj/argo-cd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: e2e for clusters (argoproj#7118)
feat: e2e for clusters (argoproj#7118) Signed-off-by: pashavictorovich <[email protected]>
- Loading branch information
1 parent
ce1d803
commit a9f009c
Showing
6 changed files
with
249 additions
and
9 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
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
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...) | ||
} |
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,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 | ||
} |
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,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 | ||
} |
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