Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Nov 22, 2023
1 parent 44e9377 commit 79012bc
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 1 deletion.
1 change: 0 additions & 1 deletion tsuru/client/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ func (c *JobOrApp) setMetadata(apiClient *tsuru.APIClient, metadata tsuru.Metada
type MetadataGet struct {
cmd.AppNameMixIn
jobName string
appProcess string
flagsApplied bool
json bool
fs *gnuflag.FlagSet
Expand Down
133 changes: 133 additions & 0 deletions tsuru/client/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,52 @@ func (s *S) TestMetadataGetJobRun(c *check.C) {
c.Assert(stdout.String(), check.Equals, result)
}

func (s *S) TestMetadataGetAppRunWithProcesses(c *check.C) {
var stdout, stderr bytes.Buffer
jsonResult := `{
"name": "somejob",
"metadata": {
"annotations": [{
"name": "my-annotation",
"value": "some long value"
}],
"labels": [{
"name": "logging.globoi.com/backup",
"value": "true"
}]
},
"processes": [
{
"name": "web",
"metadata": {
"labels": [{
"name": "logging.globoi.com/sampling",
"value": "0.1"
}]
}
}
]
}`
result := "Metadata for app:\n" +
"Labels:\n" +
"\tlogging.globoi.com/backup: true\n" +
"Annotations:\n" +
"\tmy-annotation: some long value\n\n" +
"Metadata for process: \"web\"\n" +
"Labels:\n" +
"\tlogging.globoi.com/sampling: 0.1\n"
context := cmd.Context{
Stdout: &stdout,
Stderr: &stderr,
}
client := cmd.NewClient(&http.Client{Transport: &cmdtest.Transport{Message: jsonResult, Status: http.StatusOK}}, nil, manager)
command := MetadataGet{}
command.Flags().Parse(true, []string{"-a", "someapp"})
err := command.Run(&context, client)
c.Assert(err, check.IsNil)
c.Assert(stdout.String(), check.Equals, result)
}

func (s *S) TestMetadataSetInfo(c *check.C) {
c.Assert((&MetadataSet{}).Info(), check.NotNil)
}
Expand Down Expand Up @@ -159,6 +205,49 @@ func (s *S) TestMetadataSetRunAppWithLabel(c *check.C) {
c.Assert(stdout.String(), check.Equals, expectedOut)
}

func (s *S) TestMetadataSetRunAppWithProcess(c *check.C) {
var stdout, stderr bytes.Buffer
context := cmd.Context{
Args: []string{"test.tsuru.io/label=some-value"},
Stdout: &stdout,
Stderr: &stderr,
}
expectedOut := "app \"someapp\" has been updated!\n"

trans := &cmdtest.ConditionalTransport{
Transport: cmdtest.Transport{Status: http.StatusOK},
CondFunc: func(req *http.Request) bool {
url := strings.HasSuffix(req.URL.Path, "/apps/someapp")
method := req.Method == "PUT"
data, err := io.ReadAll(req.Body)
c.Assert(err, check.IsNil)
var payload map[string]interface{}
err = json.Unmarshal(data, &payload)
c.Assert(err, check.IsNil)
c.Assert(payload, check.DeepEquals, map[string]interface{}{
"planoverride": map[string]interface{}{},
"metadata": map[string]interface{}{},
"processes": []interface{}{
map[string]interface{}{
"name": "web",
"metadata": map[string]interface{}{
"labels": []interface{}{map[string]interface{}{"name": "test.tsuru.io/label", "value": "some-value"}},
},
},
},
})
return url && method
},
}
client := cmd.NewClient(&http.Client{Transport: trans}, nil, manager)

command := MetadataSet{}
command.Flags().Parse(true, []string{"-a", "someapp", "-t", "label", "-p", "web"})
err := command.Run(&context, client)
c.Assert(err, check.IsNil)
c.Assert(stdout.String(), check.Equals, expectedOut)
}

func (s *S) TestMetadataSetRunJobWithAnnotations(c *check.C) {
var stdout, stderr bytes.Buffer
context := cmd.Context{
Expand Down Expand Up @@ -342,6 +431,50 @@ func (s *S) TestMetadataUnsetRunAppWithLabel(c *check.C) {
c.Assert(stdout.String(), check.Equals, expectedOut)
}

func (s *S) TestMetadataUnsetRunAppWithProcess(c *check.C) {
var stdout, stderr bytes.Buffer
context := cmd.Context{
Args: []string{"test.tsuru.io/label"},
Stdout: &stdout,
Stderr: &stderr,
}
expectedOut := "app \"someapp\" has been updated!\n"

trans := &cmdtest.ConditionalTransport{
Transport: cmdtest.Transport{Status: http.StatusOK},
CondFunc: func(req *http.Request) bool {
url := strings.HasSuffix(req.URL.Path, "/apps/someapp")
method := req.Method == "PUT"
data, err := io.ReadAll(req.Body)
c.Assert(err, check.IsNil)

var payload map[string]interface{}
err = json.Unmarshal(data, &payload)
c.Assert(err, check.IsNil)
c.Assert(payload, check.DeepEquals, map[string]interface{}{
"planoverride": map[string]interface{}{},
"metadata": map[string]interface{}{},
"processes": []interface{}{
map[string]interface{}{
"name": "worker",
"metadata": map[string]interface{}{
"labels": []interface{}{map[string]interface{}{"name": "test.tsuru.io/label", "delete": true}},
},
},
},
})
return url && method
},
}
client := cmd.NewClient(&http.Client{Transport: trans}, nil, manager)

command := MetadataUnset{}
command.Flags().Parse(true, []string{"-a", "someapp", "-t", "label", "-p", "worker"})
err := command.Run(&context, client)
c.Assert(err, check.IsNil)
c.Assert(stdout.String(), check.Equals, expectedOut)
}

func (s *S) TestMetadataUnsetRunJobWithAnnotations(c *check.C) {
var stdout, stderr bytes.Buffer
context := cmd.Context{
Expand Down

0 comments on commit 79012bc

Please sign in to comment.