github.com/spinnaker/spin@v1.30.0/cmd/canary/canary-config/delete_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  	"io/ioutil"
    19  	"net/http"
    20  	"net/http/httptest"
    21  	"testing"
    22  
    23  	"github.com/spinnaker/spin/cmd"
    24  	"github.com/spinnaker/spin/cmd/canary"
    25  	"github.com/spinnaker/spin/util"
    26  )
    27  
    28  func TestCanaryConfigDelete_basic(t *testing.T) {
    29  	ts := testGateDeleteSuccess()
    30  	defer ts.Close()
    31  
    32  	rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard)
    33  	canaryCmd, canaryOpts := canary.NewCanaryCmd(rootOpts)
    34  	canaryCmd.AddCommand(NewCanaryConfigCmd(canaryOpts))
    35  	rootCmd.AddCommand(canaryCmd)
    36  
    37  	args := []string{"canary", "canary-config", "delete", "configId", "--gate-endpoint", ts.URL}
    38  	rootCmd.SetArgs(args)
    39  	err := rootCmd.Execute()
    40  	if err != nil {
    41  		t.Fatalf("Command failed with: %s", err)
    42  	}
    43  }
    44  
    45  func TestCanaryConfigDelete_fail(t *testing.T) {
    46  	ts := testGateFail()
    47  	defer ts.Close()
    48  
    49  	rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard)
    50  	canaryCmd, canaryOpts := canary.NewCanaryCmd(rootOpts)
    51  	canaryCmd.AddCommand(NewCanaryConfigCmd(canaryOpts))
    52  	rootCmd.AddCommand(canaryCmd)
    53  
    54  	args := []string{"canary", "canary-config", "delete", "configId", "--gate-endpoint", ts.URL}
    55  	rootCmd.SetArgs(args)
    56  	err := rootCmd.Execute()
    57  	if err == nil {
    58  		t.Fatalf("Command failed with: %s", err)
    59  	}
    60  }
    61  
    62  func TestCanaryConfigDelete_missingid(t *testing.T) {
    63  	ts := testGateDeleteSuccess()
    64  	defer ts.Close()
    65  
    66  	rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard)
    67  	canaryCmd, canaryOpts := canary.NewCanaryCmd(rootOpts)
    68  	canaryCmd.AddCommand(NewCanaryConfigCmd(canaryOpts))
    69  	rootCmd.AddCommand(canaryCmd)
    70  
    71  	args := []string{"canary", "canary-config", "delete", "--gate-endpoint", ts.URL} // Missing cc id.
    72  	rootCmd.SetArgs(args)
    73  	err := rootCmd.Execute()
    74  	if err == nil {
    75  		t.Fatalf("Command errantly succeeded. %s", err)
    76  	}
    77  }
    78  
    79  // testGateDeleteSuccess spins up a local http server that we will configure the GateClient
    80  // to direct requests to.
    81  func testGateDeleteSuccess() *httptest.Server {
    82  	mux := util.TestGateMuxWithVersionHandler()
    83  	mux.Handle("/v2/canaryConfig/configId", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    84  		w.WriteHeader(http.StatusOK)
    85  	}))
    86  	return httptest.NewServer(mux)
    87  }