github.com/spinnaker/spin@v1.30.0/cmd/pipeline/execution/list_test.go (about)

     1  // Copyright (c) 2019, 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 execution
    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/pipeline"
    27  	"github.com/spinnaker/spin/util"
    28  )
    29  
    30  func TestExecutionList_basic(t *testing.T) {
    31  	ts := testGateExecutionListSuccess()
    32  	defer ts.Close()
    33  
    34  	rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard)
    35  	pipelineCmd, pipelineOpts := pipeline.NewPipelineCmd(rootOpts)
    36  	pipelineCmd.AddCommand(NewExecutionCmd(pipelineOpts))
    37  	rootCmd.AddCommand(pipelineCmd)
    38  
    39  	args := []string{"pipeline", "execution", "list", "--pipeline-id", "myid", "--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 TestExecutionList_flags(t *testing.T) {
    48  	ts := testGateExecutionListSuccess()
    49  	defer ts.Close()
    50  
    51  	rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard)
    52  	pipelineCmd, pipelineOpts := pipeline.NewPipelineCmd(rootOpts)
    53  	pipelineCmd.AddCommand(NewExecutionCmd(pipelineOpts))
    54  	rootCmd.AddCommand(pipelineCmd)
    55  
    56  	args := []string{"pipeline", "execution", "list", "--gate-endpoint", ts.URL} // Missing pipeline id.
    57  	rootCmd.SetArgs(args)
    58  	err := rootCmd.Execute()
    59  	if err == nil {
    60  		t.Fatalf("Command failed with: %s", err)
    61  	}
    62  }
    63  
    64  func TestExecutionList_fail(t *testing.T) {
    65  	ts := testGateFail()
    66  	defer ts.Close()
    67  
    68  	rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard)
    69  	pipelineCmd, pipelineOpts := pipeline.NewPipelineCmd(rootOpts)
    70  	pipelineCmd.AddCommand(NewExecutionCmd(pipelineOpts))
    71  	rootCmd.AddCommand(pipelineCmd)
    72  
    73  	args := []string{"pipeline", "execution", "list", "--pipeline-id", "myid", "--gate-endpoint", ts.URL}
    74  	rootCmd.SetArgs(args)
    75  	err := rootCmd.Execute()
    76  	if err == nil {
    77  		t.Fatalf("Command failed with: %s", err)
    78  	}
    79  }
    80  
    81  // testGateExecutionListSuccess spins up a local http server that we will configure the GateClient
    82  // to direct requests to. Responds with a 200 and a well-formed execution list.
    83  func testGateExecutionListSuccess() *httptest.Server {
    84  	mux := util.TestGateMuxWithVersionHandler()
    85  	mux.Handle("/executions/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    86  		fmt.Fprintln(w, strings.TrimSpace(executionListJson))
    87  	}))
    88  	return httptest.NewServer(mux)
    89  }
    90  
    91  const executionListJson = `
    92  [
    93   {
    94    "application": "myapp",
    95    "authentication": {
    96     "allowedAccounts": [
    97      "spinnaker-gce",
    98      "my-docker-registry",
    99      "default"
   100     ],
   101     "user": "anonymous"
   102    },
   103    "buildTime": 1550860667806,
   104    "canceled": false,
   105    "endTime": 1550860701046,
   106    "id": "01D4B7ZJWYANYN45AWMK4RNTYN",
   107    "initialConfig": {},
   108    "keepWaitingPipelines": false,
   109    "limitConcurrent": true,
   110    "name": "param wait",
   111    "notifications": [],
   112    "origin": "deck",
   113    "pipelineConfigId": "d34db81c-b2f1-4748-b660-609a77945d51",
   114    "stages": [
   115     {
   116      "context": {
   117       "comments": "2019-03-02",
   118       "startTime": 1550860670135,
   119       "waitTime": "30"
   120      },
   121      "endTime": 1550860700773,
   122      "id": "01D4B7ZJWYK838WRP9PD44KKT3",
   123      "name": "Wait",
   124      "outputs": {},
   125      "refId": "1",
   126      "requisiteStageRefIds": [
   127       "2"
   128      ],
   129      "startTime": 1550860669658,
   130      "status": "SUCCEEDED",
   131      "tasks": [
   132       {
   133        "endTime": 1550860700539,
   134        "id": "1",
   135        "implementingClass": "com.netflix.spinnaker.orca.pipeline.tasks.WaitTask",
   136        "loopEnd": false,
   137        "loopStart": false,
   138        "name": "wait",
   139        "stageEnd": true,
   140        "stageStart": true,
   141        "startTime": 1550860669893,
   142        "status": "SUCCEEDED"
   143       }
   144      ],
   145      "type": "wait"
   146     },
   147     {
   148      "context": {
   149       "failOnFailedExpressions": true,
   150       "variables": [
   151        {
   152         "key": "theDate",
   153         "value": "2019-03-02"
   154        }
   155       ]
   156      },
   157      "endTime": 1550860669437,
   158      "id": "01D4B7ZJWY2WRC7K1QGKS6DQXF",
   159      "name": "Evaluate Variables",
   160      "outputs": {
   161       "theDate": "2019-03-02"
   162      },
   163      "refId": "2",
   164      "requisiteStageRefIds": [],
   165      "startTime": 1550860668515,
   166      "status": "SUCCEEDED",
   167      "tasks": [
   168       {
   169        "endTime": 1550860669203,
   170        "id": "1",
   171        "implementingClass": "com.netflix.spinnaker.orca.pipeline.tasks.EvaluateVariablesTask",
   172        "loopEnd": false,
   173        "loopStart": false,
   174        "name": "evaluateVariables",
   175        "stageEnd": true,
   176        "stageStart": true,
   177        "startTime": 1550860668749,
   178        "status": "SUCCEEDED"
   179       }
   180      ],
   181      "type": "evaluateVariables"
   182     }
   183    ],
   184    "startTime": 1550860668270,
   185    "status": "SUCCEEDED",
   186    "systemNotifications": [],
   187    "trigger": {
   188     "artifacts": [],
   189     "dryRun": false,
   190     "notifications": [],
   191     "parameters": {},
   192     "rebake": false,
   193     "resolvedExpectedArtifacts": [],
   194     "strategy": false,
   195     "type": "manual",
   196     "user": "anonymous"
   197    },
   198    "type": "PIPELINE"
   199   }
   200  ]
   201  `