github.com/spinnaker/spin@v1.30.0/cmd/application/delete_test.go (about)

     1  // Copyright (c) 2018, Google, 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 application
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"io/ioutil"
    21  	"net/http"
    22  	"net/http/httptest"
    23  	"testing"
    24  
    25  	"github.com/spinnaker/spin/cmd"
    26  	"github.com/spinnaker/spin/util"
    27  )
    28  
    29  func TestApplicationDelete_basic(t *testing.T) {
    30  	ts := testGateApplicationDeleteSuccess()
    31  	defer ts.Close()
    32  
    33  	rootCmd, options := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard)
    34  	rootCmd.AddCommand(NewApplicationCmd(options))
    35  
    36  	args := []string{"application", "delete", NAME, "--gate-endpoint=" + ts.URL}
    37  	rootCmd.SetArgs(args)
    38  	err := rootCmd.Execute()
    39  	if err != nil {
    40  		t.Fatalf("Command failed with: %s", err)
    41  	}
    42  }
    43  
    44  func TestApplicationDelete_fail(t *testing.T) {
    45  	ts := GateAppDeleteFail()
    46  	defer ts.Close()
    47  
    48  	rootCmd, options := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard)
    49  	rootCmd.AddCommand(NewApplicationCmd(options))
    50  
    51  	args := []string{"application", "delete", NAME, "--gate-endpoint=" + ts.URL}
    52  	rootCmd.SetArgs(args)
    53  	err := rootCmd.Execute()
    54  	if err == nil {
    55  		t.Fatalf("Command failed with: %s", err)
    56  	}
    57  }
    58  
    59  func TestApplicationDelete_flags(t *testing.T) {
    60  	ts := testGateApplicationDeleteSuccess()
    61  	defer ts.Close()
    62  
    63  	rootCmd, options := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard)
    64  	rootCmd.AddCommand(NewApplicationCmd(options))
    65  
    66  	args := []string{"application", "delete", NAME, "--gate-endpoint=" + ts.URL}
    67  	rootCmd.SetArgs(args)
    68  	err := rootCmd.Execute()
    69  	if err != nil {
    70  		t.Fatalf("Command failed with: %s", err)
    71  	}
    72  }
    73  
    74  // testGateApplicationDeleteSuccess spins up a local http server that we will configure the GateClient
    75  // to direct requests to. Responds with successful responses to pipeline execute API calls.
    76  func testGateApplicationDeleteSuccess() *httptest.Server {
    77  	mux := util.TestGateMuxWithVersionHandler()
    78  	mux.Handle("/applications/"+APP, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    79  		payload := map[string]string{} // We don't use the payload, we are just checking if the target app exists.
    80  		b, _ := json.Marshal(&payload)
    81  		w.Header().Add("content-type", "application/json")
    82  		fmt.Fprintln(w, string(b))
    83  	}))
    84  	mux.Handle("/tasks", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    85  		payload := map[string]string{
    86  			"ref": "/tasks/id",
    87  		}
    88  		b, _ := json.Marshal(&payload)
    89  		w.Header().Add("content-type", "application/json")
    90  		fmt.Fprintln(w, string(b))
    91  	}))
    92  	mux.Handle("/tasks/id", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    93  		payload := map[string]string{
    94  			"status": "SUCCEEDED",
    95  		}
    96  		b, _ := json.Marshal(&payload)
    97  		w.Header().Add("content-type", "application/json")
    98  		fmt.Fprintln(w, string(b))
    99  	}))
   100  	return httptest.NewServer(mux)
   101  }
   102  
   103  // GateAppDeleteFail spins up a local http server that we will configure the GateClient
   104  // to direct requests to. Responds with a 500 InternalServerError.
   105  func GateAppDeleteFail() *httptest.Server {
   106  	mux := util.TestGateMuxWithVersionHandler()
   107  	mux.Handle("/applications/"+APP, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   108  		// TODO(jacobkiefer): Mock more robust errors once implemented upstream.
   109  		http.Error(w, "Internal Server Error", http.StatusInternalServerError)
   110  	}))
   111  	return httptest.NewServer(mux)
   112  }