github.com/drycc/workflow-cli@v1.5.3-0.20240322092846-d4ee25983af9/cmd/labels_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"net/http"
     7  	"testing"
     8  
     9  	"github.com/drycc/controller-sdk-go/api"
    10  	"github.com/drycc/workflow-cli/pkg/testutil"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestLabelsList(t *testing.T) {
    15  	t.Parallel()
    16  	cf, server, err := testutil.NewTestServerAndClient()
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  	defer server.Close()
    21  	var b bytes.Buffer
    22  	cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
    23  
    24  	server.Mux.HandleFunc("/v2/apps/rivendell/settings/", func(w http.ResponseWriter, _ *http.Request) {
    25  		testutil.SetHeaders(w)
    26  		fmt.Fprintf(w, `{
    27  			"owner": "jim",
    28  			"app": "rivendell",
    29  		    "label": {"team" : "drycc", "git_repo": "https://github.com/drycc/controller-sdk-go"},
    30  			"created": "2014-01-01T00:00:00UTC",
    31  			"updated": "2014-01-01T00:00:00UTC",
    32  			"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"
    33  		}`)
    34  	})
    35  
    36  	err = cmdr.LabelsList("rivendell")
    37  	assert.NoError(t, err)
    38  	assert.Equal(t, b.String(), `UUID                                    OWNER    KEY         VALUE                                      
    39  de1bf5b5-4a72-4f94-a10c-d2a3741cdf75    jim      git_repo    https://github.com/drycc/controller-sdk-go    
    40  de1bf5b5-4a72-4f94-a10c-d2a3741cdf75    jim      team        drycc                                         
    41  `, "output")
    42  
    43  	server.Mux.HandleFunc("/v2/apps/mordor/settings/", func(w http.ResponseWriter, _ *http.Request) {
    44  		testutil.SetHeaders(w)
    45  		fmt.Fprintf(w, `{
    46  			"owner": "priw",
    47  			"app": "mordor",
    48  			"created": "2014-01-01T00:00:00UTC",
    49  			"updated": "2014-01-01T00:00:00UTC",
    50  			"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"
    51  		}`)
    52  	})
    53  	b.Reset()
    54  
    55  	err = cmdr.LabelsList("mordor")
    56  	assert.NoError(t, err)
    57  	assert.Equal(t, b.String(), "No labels found in mordor app.\n", "output")
    58  }
    59  
    60  func TestListsSet(t *testing.T) {
    61  	t.Parallel()
    62  	cf, server, err := testutil.NewTestServerAndClient()
    63  	if err != nil {
    64  		t.Fatal(err)
    65  	}
    66  	defer server.Close()
    67  	var b bytes.Buffer
    68  	cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
    69  
    70  	server.Mux.HandleFunc("/v2/apps/lothlorien/settings/", func(w http.ResponseWriter, r *http.Request) {
    71  		testutil.SetHeaders(w)
    72  		data := map[string]interface{}{
    73  			"git_repo": "https://github.com/drycc/controller-sdk-go",
    74  			"team":     "drycc",
    75  		}
    76  		testutil.AssertBody(t, api.AppSettings{Label: data}, r)
    77  		fmt.Fprintf(w, "{}")
    78  	})
    79  
    80  	err = cmdr.LabelsSet("lothlorien", []string{
    81  		"team=drycc",
    82  		"git_repo=https://github.com/drycc/controller-sdk-go",
    83  	})
    84  	assert.NoError(t, err)
    85  	assert.Equal(t, testutil.StripProgress(b.String()), "Applying labels on lothlorien... done\n", "output")
    86  }
    87  
    88  func TestListsUnset(t *testing.T) {
    89  	t.Parallel()
    90  	cf, server, err := testutil.NewTestServerAndClient()
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  	defer server.Close()
    95  	var b bytes.Buffer
    96  	cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
    97  
    98  	server.Mux.HandleFunc("/v2/apps/bree/settings/", func(w http.ResponseWriter, r *http.Request) {
    99  		testutil.SetHeaders(w)
   100  		testutil.AssertBody(t, api.AppSettings{Label: map[string]interface{}{
   101  			"team":     nil,
   102  			"git_repo": nil,
   103  		}}, r)
   104  		fmt.Fprintf(w, "{}")
   105  	})
   106  
   107  	err = cmdr.LabelsUnset("bree", []string{
   108  		"team",
   109  		"git_repo",
   110  	})
   111  	assert.NoError(t, err)
   112  	assert.Equal(t, testutil.StripProgress(b.String()), "Removing labels on bree... done\n", "output")
   113  }