github.com/spinnaker/spin@v1.30.0/cmd/canary/canary-config/list_test.go (about)

     1  // Copyright (c) 2019, Waze, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //   http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  //   Unless required by applicable law or agreed to in writing, software
    10  //   distributed under the License is distributed on an "AS IS" BASIS,
    11  //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //   See the License for the specific language governing permissions and
    13  //   limitations under the License.
    14  
    15  package canary_config
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"net/http"
    21  	"net/http/httptest"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/spinnaker/spin/cmd"
    26  	"github.com/spinnaker/spin/cmd/canary"
    27  	"github.com/spinnaker/spin/util"
    28  )
    29  
    30  func TestCanaryConfigList_basic(t *testing.T) {
    31  	ts := testGateCanaryConfigListSuccess()
    32  	defer ts.Close()
    33  
    34  	rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard)
    35  	canaryCmd, canaryOpts := canary.NewCanaryCmd(rootOpts)
    36  	canaryCmd.AddCommand(NewCanaryConfigCmd(canaryOpts))
    37  	rootCmd.AddCommand(canaryCmd)
    38  
    39  	args := []string{"canary", "canary-config", "list", "--gate-endpoint", ts.URL}
    40  	rootCmd.SetArgs(args)
    41  	err := rootCmd.Execute()
    42  	if err != nil {
    43  		t.Fatalf("Command failed with: %s", err)
    44  	}
    45  }
    46  
    47  func TestCanaryConfigList_fail(t *testing.T) {
    48  	ts := testGateFail()
    49  	defer ts.Close()
    50  
    51  	rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard)
    52  	canaryCmd, canaryOpts := canary.NewCanaryCmd(rootOpts)
    53  	canaryCmd.AddCommand(NewCanaryConfigCmd(canaryOpts))
    54  	rootCmd.AddCommand(canaryCmd)
    55  
    56  	args := []string{"canary", "canary-config", "list", "--gate-endpoint", ts.URL}
    57  	rootCmd.SetArgs(args)
    58  	err := rootCmd.Execute()
    59  	if err == nil {
    60  		t.Fatalf("Command failed with: %s", err)
    61  	}
    62  }
    63  
    64  // testGateCanaryConfigListSuccess spins up a local http server that we will configure the GateClient
    65  // to direct requests to. Responds with a 200 and a well-formed canaryConfig list.
    66  func testGateCanaryConfigListSuccess() *httptest.Server {
    67  	mux := util.TestGateMuxWithVersionHandler()
    68  	mux.Handle(
    69  		"/v2/canaryConfig",
    70  		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    71  			w.Header().Add("content-type", "application/json")
    72  			fmt.Fprintln(w, strings.TrimSpace(canaryConfigListJson))
    73  		}))
    74  	return httptest.NewServer(mux)
    75  }
    76  
    77  // testGateFail spins up a local http server that we will configure the GateClient
    78  // to direct requests to. Responds with a 500 InternalServerError.
    79  func testGateFail() *httptest.Server {
    80  	return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    81  		w.Header().Add("content-type", "application/json")
    82  		http.Error(w, "Internal Server Error", http.StatusInternalServerError)
    83  	}))
    84  }
    85  
    86  const canaryConfigListJson = `
    87  [
    88   {
    89    "applications": [
    90     "canaryconfigs"
    91    ],
    92    "id": "3f3dbcc1-002d-458c-b181-be4aa809922a",
    93    "name": "exampleCanary",
    94    "updatedTimestamp": 1568131247595,
    95    "updatedTimestampIso": "2019-09-10T16:00:47.595Z"
    96   }
    97  ]
    98  `