github.com/spinnaker/spin@v1.30.0/cmd/pipeline/list_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 pipeline
    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/util"
    27  )
    28  
    29  func TestPipelineList_basic(t *testing.T) {
    30  	ts := testGatePipelineListSuccess()
    31  	defer ts.Close()
    32  
    33  	rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard)
    34  	pipelineCmd, _ := NewPipelineCmd(rootOpts)
    35  	rootCmd.AddCommand(pipelineCmd)
    36  
    37  	args := []string{"pipeline", "list", "--application", "app", "--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 TestPipelineList_flags(t *testing.T) {
    46  	ts := testGatePipelineListSuccess()
    47  	defer ts.Close()
    48  
    49  	rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard)
    50  	pipelineCmd, _ := NewPipelineCmd(rootOpts)
    51  	rootCmd.AddCommand(pipelineCmd)
    52  
    53  	args := []string{"pipeline", "list", "--gate-endpoint", ts.URL} // Missing application.
    54  	rootCmd.SetArgs(args)
    55  	err := rootCmd.Execute()
    56  	if err == nil {
    57  		t.Fatalf("Command failed with: %s", err)
    58  	}
    59  }
    60  
    61  func TestPipelineList_fail(t *testing.T) {
    62  	ts := testGateFail()
    63  	defer ts.Close()
    64  
    65  	rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard)
    66  	pipelineCmd, _ := NewPipelineCmd(rootOpts)
    67  	rootCmd.AddCommand(pipelineCmd)
    68  
    69  	args := []string{"pipeline", "list", "--application", "app", "--gate-endpoint", ts.URL}
    70  	rootCmd.SetArgs(args)
    71  	err := rootCmd.Execute()
    72  	if err == nil {
    73  		t.Fatalf("Command failed with: %s", err)
    74  	}
    75  }
    76  
    77  // testGatePipelineListSuccess spins up a local http server that we will configure the GateClient
    78  // to direct requests to. Responds with a 200 and a well-formed pipeline list.
    79  func testGatePipelineListSuccess() *httptest.Server {
    80  	mux := util.TestGateMuxWithVersionHandler()
    81  	mux.Handle("/applications/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    82  		fmt.Fprintln(w, strings.TrimSpace(pipelineListJson))
    83  	}))
    84  	return httptest.NewServer(mux)
    85  }
    86  
    87  const pipelineListJson = `
    88  [
    89    {
    90      "application": "app",
    91      "id": "pipeline1",
    92      "index": 0,
    93      "keepWaitingPipelines": false,
    94      "lastModifiedBy": "jacobkiefer@google.com",
    95      "limitConcurrent": true,
    96      "name": "derp1",
    97      "parameterConfig": [
    98        {
    99          "default": "bar",
   100          "description": "A foo.",
   101          "name": "foo",
   102          "required": true
   103        }
   104      ],
   105      "stages": [
   106        {
   107          "comments": "${ parameters.derp }",
   108          "name": "Wait",
   109          "refId": "1",
   110          "requisiteStageRefIds": [],
   111          "type": "wait",
   112          "waitTime": 30
   113        }
   114      ],
   115      "triggers": [],
   116      "updateTs": "1526578883109"
   117    }
   118  ]
   119  `