Skip to content

Commit

Permalink
improve testing tool
Browse files Browse the repository at this point in the history
Signed-off-by: pashavictorovich <[email protected]>
  • Loading branch information
pasha-codefresh committed Jan 26, 2022
1 parent b707f4b commit 7405e03
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 5 deletions.
72 changes: 69 additions & 3 deletions hack/chaos-testing/cmd/commands/cmd.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package commands

import (
"fmt"

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

util2 "github.com/argoproj/argo-cd/v2/util"

"github.com/spf13/cobra"

"github.com/argoproj/argo-cd/v2/hack/chaos-testing/util"

cmdutil "github.com/argoproj/argo-cd/v2/cmd/util"
"github.com/argoproj/argo-cd/v2/hack/gen-resources/util"
"github.com/argoproj/argo-cd/v2/util/cli"
)

Expand All @@ -21,20 +28,79 @@ func initConfig() {
cli.SetLogLevel(cmdutil.LogLevel)
}

func simulateReplicasChanges(token string, argoApi *util.ArgoApi, app v1alpha1.Application) {

fmt.Printf("Pick application with name %s \n", app.Name)

i := 0
for {
fmt.Printf("Execute replication emulation number: %v \n", i)

fmt.Println("Scale to 2")

_, err := argoApi.UpdateReplicas(token, 2, app.Name)
if err != nil {
fmt.Printf(err.Error())
return
}

util2.Wait(30, func(bools chan<- bool) {

})

fmt.Println("Scale to 1")

_, err = argoApi.UpdateReplicas(token, 1, app.Name)
if err != nil {
fmt.Printf(err.Error())
return
}

i++

}

}

// NewCommand returns a new instance of an argocd command
func NewCommand() *cobra.Command {

var generateOpts util.GenerateOpts
var host, username, password string
var threads int

var command = &cobra.Command{
Use: cliName,
Short: "Chaos testing",
Run: func(c *cobra.Command, args []string) {
argoApi := util.NewArgoApi(host)
token, err := argoApi.GetToken(username, password)
if err != nil {
return
}

apps, err := argoApi.ListApplications(token)
if err != nil {
fmt.Printf(err.Error())
return
}

for i := 0; i < threads; i++ {
_, _ = argoApi.Sync(token, apps[i].Name)
util2.Wait(5, func(bools chan<- bool) {
})
go simulateReplicasChanges(token, argoApi, apps[i])
}

select {}

},
DisableAutoGenTag: true,
}

command.PersistentFlags().StringVar(&generateOpts.Namespace, "kube-namespace", "argocd", "Name of the namespace on which Argo agent should be installed [$KUBE_NAMESPACE]")
command.PersistentFlags().StringVar(&host, "host", "http://localhost:8080", "Argo server host")
command.PersistentFlags().StringVar(&username, "username", "admin", "Username")
command.PersistentFlags().StringVar(&password, "password", "", "Argo server host")
command.PersistentFlags().IntVar(&threads, "threads", 1, "Amount of threads")

return command
}
2 changes: 1 addition & 1 deletion hack/chaos-testing/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"os"

"github.com/argoproj/argo-cd/v2/hack/gen-resources/cmd/commands"
"github.com/argoproj/argo-cd/v2/hack/chaos-testing/cmd/commands"
)

func main() {
Expand Down
109 changes: 108 additions & 1 deletion hack/chaos-testing/util/argo-api.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,111 @@
package util

type argoApi struct {
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"

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

type ArgoApi struct {
host string
}

func NewArgoApi(host string) *ArgoApi {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
return &ArgoApi{host}
}

func (api *ArgoApi) sendPost(uri string, token string, payload []byte) (map[string]interface{}, error) {
client := &http.Client{}
req, err := http.NewRequest("POST", api.host+uri, bytes.NewBuffer(payload))
if err != nil {
return nil, err
}
if token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
result := make(map[string]interface{})
body, err := io.ReadAll(resp.Body)
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
return result, nil
}

func (api *ArgoApi) sendGet(uri string, token string, result interface{}) error {
client := &http.Client{}
req, err := http.NewRequest("GET", api.host+uri, nil)
if err != nil {
return err
}
if token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
err = json.Unmarshal(body, &result)
if err != nil {
return err
}
return nil
}

func (api *ArgoApi) GetToken(username, password string) (string, error) {
payload := make(map[string]interface{})
payload["username"] = username
payload["password"] = password
postBody, err := json.Marshal(payload)
if err != nil {
return "", err
}
result, err := api.sendPost("/api/v1/session", "", postBody)
if err != nil {
return "", err
}
return result["token"].(string), nil
}

func (api *ArgoApi) ListApplications(token string) ([]v1alpha1.Application, error) {
var apps v1alpha1.ApplicationList
err := api.sendGet("/api/v1/applications", token, &apps)
if err != nil {
return nil, err
}
return apps.Items, nil
}

func (api *ArgoApi) UpdateReplicas(token string, replicasCount int, applicationName string) (map[string]interface{}, error) {
data := fmt.Sprintf("\"{\\\"spec\\\":{\\\"replicas\\\":%d}}\"", replicasCount)
uri := strings.ReplaceAll("/api/v1/applications/%s/resource?name=%s-helm-guestbook&namespace=argocd&resourceName=%s-helm-guestbook&version=v1&kind=Deployment&group=apps&patchType=application%2Fmerge-patch%2Bjson", "%s", applicationName)
result, err := api.sendPost(uri, token, []byte(data))
if err != nil {
return nil, err
}
return result, nil
}

func (api *ArgoApi) Sync(token string, applicationName string) (map[string]interface{}, error) {
data := `{"revision":"master","prune":false,"dryRun":false,"strategy":{"hook":{"force":false}},"resources":null,"syncOptions":{"items":["CreateNamespace=true"]}}`
uri := fmt.Sprintf("/api/v1/applications/%s/sync", applicationName)
result, err := api.sendPost(uri, token, []byte(data))
if err != nil {
return nil, err
}
return result, nil
}

0 comments on commit 7405e03

Please sign in to comment.