Skip to content

Commit

Permalink
more changes
Browse files Browse the repository at this point in the history
  • Loading branch information
yindia committed Jan 2, 2025
1 parent 0e1802a commit b2ee8c7
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 143 deletions.
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ if err != nil {
A `Workflow` consists of multiple steps that are executed in sequence. If any step fails its assertions, the workflow stops.

```go
workflow := iapetus.Workflow{
workflow := iapetus.Workflow{
Name: "Entire flow",
PreRun: func(w *iapetus.Workflow) error {
//# Do sonething
//# Do sonething like setup kind cluster
return nil
},
LogLevel: 1,
Expand Down Expand Up @@ -137,12 +137,10 @@ workflow := iapetus.Workflow{
},
},
},
}

err := workflow.Run()
if err != nil {
log.Fatalf("Failed to run workflow: %v", err)
}
}
if err := workflow.Run(); err != nil {
log.Fatalf("Failed to run workflow: %v", err)
}
```

### Builder Pattern
Expand Down
251 changes: 120 additions & 131 deletions example/kubectl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,106 @@ import (

const ns = "test"

func assertPodsLength(s *iapetus.Task) error {
var TASK_CREATE_KIND_CLUSTER = iapetus.Task{
Name: "Create Kind Cluster",
Command: "kind",
Args: []string{"create", "cluster"},
Env: []string{},
Expected: iapetus.Output{
ExitCode: 0,
},
Asserts: []func(*iapetus.Task) error{
iapetus.AssertByExitCode,
},
}

var TASK_DELETE_KIND_CLUSTER = iapetus.Task{
Name: "Delete Kind Cluster",
Command: "kind",
Args: []string{"delete", "cluster"},
Env: []string{},
Expected: iapetus.Output{
ExitCode: 0,
},
Asserts: []func(*iapetus.Task) error{
iapetus.AssertByExitCode,
},
}

var TASK_CREATE_NGINX_DEPLOYMENT = iapetus.Task{
Name: "kubectl-create-deployment",
Command: "kubectl",
Args: []string{"create", "deployment", "test", "--image", "nginx", "--replicas", "30", "-n", ns},
Env: []string{},
Expected: iapetus.Output{
ExitCode: 0,
},
Asserts: []func(*iapetus.Task) error{
iapetus.AssertByExitCode,
},
}

var TASK_DELETE_NGINX_DEPLOYMENT = iapetus.Task{
Name: "kubectl-delete-deployment",
Command: "kubectl",
Args: []string{"delete", "deployment", "test", "-n", ns},
Env: []string{},
Expected: iapetus.Output{
ExitCode: 0,
},
Asserts: []func(*iapetus.Task) error{
iapetus.AssertByExitCode,
},
}

var TASK_DELETE_NS = iapetus.Task{
Name: "kubectl-delete-ns",
Command: "kubectl",
Args: []string{"delete", "ns", ns},
Env: []string{},
Expected: iapetus.Output{
ExitCode: 0,
},
}

var TASK_CREATE_NS = iapetus.Task{
Name: "kubectl-create-ns",
Command: "kubectl",
Args: []string{"create", "ns", ns},
Env: []string{},
Expected: iapetus.Output{
ExitCode: 0,
},
Asserts: []func(*iapetus.Task) error{
iapetus.AssertByExitCode,
},
}

func AssertByCustomDeploymentCheck(s *iapetus.Task) error {
deployment := &appsv1.DeploymentList{}
err := json.Unmarshal([]byte(s.Actual.Output), &deployment)
if err != nil {
return fmt.Errorf("failed to unmarshal deployment specs: %w", err)
}
if len(deployment.Items) == 1 {
return fmt.Errorf("deployment length should be 1")
}
for _, item := range deployment.Items {
if item.Name == "test" {
for _, container := range item.Spec.Template.Spec.Containers {
if container.Image != "nginx" {
return fmt.Errorf("container image should be nginx")
}
}
if item.Status.Replicas != *item.Spec.Replicas {
return fmt.Errorf("deployment replicas do not match desired state")
}
}
}
return nil
}

func AssertByPodsCount(s *iapetus.Task) error {
pods := &v1.PodList{}
err := json.Unmarshal([]byte(s.Actual.Output), &pods)
if err != nil {
Expand All @@ -25,7 +124,7 @@ func assertPodsLength(s *iapetus.Task) error {
return nil
}

func assertNsLength(s *iapetus.Task) error {
func AssertByNsCount(s *iapetus.Task) error {
ns := &v1.Namespace{}
err := json.Unmarshal([]byte(s.Actual.Output), &ns)
if err != nil {
Expand All @@ -51,115 +150,38 @@ func main() {
os.Exit(1)
}
}
teardownWorkflow := teardownWorkflow()
if err := teardownWorkflow.Run(); err != nil {

if err := TASK_DELETE_KIND_CLUSTER.Run(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func setupWorkflow() iapetus.Workflow {
return iapetus.Workflow{
Name: "Setup",
LogLevel: 1,
Steps: []iapetus.Task{
{
Name: "Create Kind Cluster",
Command: "kind",
Args: []string{"create", "cluster"},
Env: []string{},
Expected: iapetus.Output{
ExitCode: 0,
},
Asserts: []func(*iapetus.Task) error{
iapetus.AssertByExitCode,
},
},
},
}
}

func teardownWorkflow() iapetus.Workflow {
return iapetus.Workflow{
Name: "Cleanup",
LogLevel: 1,
Steps: []iapetus.Task{
{
Name: "Delete Kind Cluster",
Command: "kind",
Args: []string{"delete", "cluster"},
Env: []string{},
Expected: iapetus.Output{
ExitCode: 0,
},
Asserts: []func(*iapetus.Task) error{
iapetus.AssertByExitCode,
},
},
},
}
}

func getWorkflowCasesForKubernetes() iapetus.Workflow {
return iapetus.Workflow{
Name: "Entire flow",
PreRun: func(w *iapetus.Workflow) error {
setupWorkflowSpec := setupWorkflow()
if err := setupWorkflowSpec.Run(); err != nil {
return err
}
return nil
return TASK_CREATE_KIND_CLUSTER.Run()
},
LogLevel: 1,
PostRun: func(w *iapetus.Workflow) error {
return TASK_DELETE_NS.Run()
},
Steps: []iapetus.Task{
{
Name: "kubectl-create-ns",
Command: "kubectl",
Args: []string{"create", "ns", ns},
Env: []string{},
Expected: iapetus.Output{
ExitCode: 0,
},
Asserts: []func(*iapetus.Task) error{
iapetus.AssertByExitCode,
},
},
{
Name: "kubectl-get-ns",
Command: "kubectl",
Args: []string{"get", "ns", ns, "-o", "json"},
Env: []string{},
Expected: iapetus.Output{
ExitCode: 0,
},
Asserts: []func(*iapetus.Task) error{
iapetus.AssertByExitCode,
assertNsLength,
PreRun: func(w *iapetus.Task) error {
return TASK_CREATE_NS.Run()
},
},
{
Name: "kubectl-get-pods",
Command: "kubectl",
Args: []string{"get", "pods", "-n", ns, "-o", "json"},
Env: []string{},
Expected: iapetus.Output{
ExitCode: 0,
},
Asserts: []func(*iapetus.Task) error{
iapetus.AssertByExitCode,
assertPodsLength,
},
},
{
Name: "kubectl-create-deployment",
Command: "kubectl",
Args: []string{"create", "deployment", "test", "--image", "nginx", "--replicas", "30", "-n", ns},
Env: []string{},
Expected: iapetus.Output{
ExitCode: 0,
},
Asserts: []func(*iapetus.Task) error{
iapetus.AssertByExitCode,
AssertByNsCount,
},
},
{
Expand All @@ -168,55 +190,18 @@ func getWorkflowCasesForKubernetes() iapetus.Workflow {
Args: []string{"get", "pods", "-n", ns, "-o", "json"},
Env: []string{},
Retries: 1,
Expected: iapetus.Output{
ExitCode: 0,
PreRun: func(w *iapetus.Task) error {
return TASK_CREATE_NGINX_DEPLOYMENT.Run()
},
Asserts: []func(*iapetus.Task) error{
iapetus.AssertByExitCode,
func(s *iapetus.Task) error {
deployment := &appsv1.DeploymentList{}
err := json.Unmarshal([]byte(s.Actual.Output), &deployment)
if err != nil {
return fmt.Errorf("failed to unmarshal deployment specs: %w", err)
}
if len(deployment.Items) == 1 {
return fmt.Errorf("deployment length should be 1")
}
for _, item := range deployment.Items {
if item.Name == "test" {
for _, container := range item.Spec.Template.Spec.Containers {
if container.Image != "nginx" {
return fmt.Errorf("container image should be nginx")
}
}
if item.Status.Replicas != *item.Spec.Replicas {
return fmt.Errorf("deployment replicas do not match desired state")
}
}
}
return nil
},
PostRun: func(w *iapetus.Task) error {
return TASK_DELETE_NGINX_DEPLOYMENT.Run()
},
},
{
Name: "kubectl-delete-deployment",
Command: "kubectl",
Args: []string{"delete", "deployment", "test", "-n", ns},
Env: []string{},
Expected: iapetus.Output{
ExitCode: 0,
},
Asserts: []func(*iapetus.Task) error{
iapetus.AssertByExitCode,
},
},
{
Name: "kubectl-delete-ns",
Command: "kubectl",
Args: []string{"delete", "ns", ns},
Env: []string{},
Expected: iapetus.Output{
ExitCode: 0,
AssertByCustomDeploymentCheck,
},
},
},
Expand All @@ -230,6 +215,10 @@ func getTestCasesForKubernetes() []iapetus.Task {
Command: "kubectl",
Args: []string{"get", "pods", "-n", "default"},
Env: []string{},
PreRun: func(t *iapetus.Task) error {
fmt.Println("Task Started, I can create a temp file before running a command")
return nil
},
Expected: iapetus.Output{
ExitCode: 0,
},
Expand All @@ -247,7 +236,7 @@ func getTestCasesForKubernetes() []iapetus.Task {
},
Asserts: []func(*iapetus.Task) error{
iapetus.AssertByExitCode,
assertPodsLength,
AssertByPodsCount,
},
},
}
Expand Down
4 changes: 1 addition & 3 deletions logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ func (l *DefaultLogger) Debug(format string, args ...interface{}) {
}

func (l *DefaultLogger) Info(format string, args ...interface{}) {
if l.level == INFO {
l.logger.Printf("[INFO] "+format, args...)
}
l.logger.Printf("[INFO] "+format, args...)
}

func (l *DefaultLogger) Error(format string, args ...interface{}) {
Expand Down
Loading

0 comments on commit b2ee8c7

Please sign in to comment.