Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cloudEvent resource support to start command #452

Merged
merged 1 commit into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions pkg/cmd/pipeline/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type resourceOptionsFilter struct {
cluster []string
storage []string
pullRequest []string
cloudEvent []string
}

// NameArg validates that the first argument is a valid pipeline name
Expand Down Expand Up @@ -237,10 +238,9 @@ func (opt *startOptions) getInputResources(resources resourceOptionsFilter, pipe
return err
}
opt.Resources = append(opt.Resources, res.Name+"="+newres.Name)
} else {
name := strings.TrimSpace(strings.Split(ans, " ")[0])
opt.Resources = append(opt.Resources, res.Name+"="+name)
}
name := strings.TrimSpace(strings.Split(ans, " ")[0])
opt.Resources = append(opt.Resources, res.Name+"="+name)
}
return nil
}
Expand Down Expand Up @@ -329,6 +329,13 @@ func getPipelineResourcesByFormat(resources []v1alpha1.PipelineResource) (ret re
}
}
ret.cluster = append(ret.cluster, fmt.Sprintf("%s (%s)", res.Name, output))
case "cloudEvent":
for _, param := range res.Spec.Params {
if param.Name == "targetURI" {
output = param.Value + output
}
}
ret.cloudEvent = append(ret.cloudEvent, fmt.Sprintf("%s (%s)", res.Name, output))
}
}
return
Expand All @@ -350,6 +357,9 @@ func getOptionsByType(resources resourceOptionsFilter, restype string) []string
if restype == "storage" {
return resources.storage
}
if restype == "cloudEvent" {
return resources.cloudEvent
}
return []string{}
}

Expand Down Expand Up @@ -566,6 +576,7 @@ func (opt *startOptions) createPipelineResource(resName string, resType v1alpha1
v1alpha1.PipelineResourceTypeImage: res.AskImageParams,
v1alpha1.PipelineResourceTypeCluster: res.AskClusterParams,
v1alpha1.PipelineResourceTypePullRequest: res.AskPullRequestParams,
v1alpha1.PipelineResourceTypeCloudEvent: res.AskCloudEventParams,
}
if res.PipelineResource.Spec.Type != "" {
if err := resourceTypeParams[res.PipelineResource.Spec.Type](); err != nil {
Expand Down
106 changes: 106 additions & 0 deletions pkg/cmd/pipeline/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,11 @@ func Test_getPipelineResourceByFormat(t *testing.T) {
tb.PipelineResourceSpecParam("location", "/home/tektoncd"),
),
),
tb.PipelineResource("scaffold-cloud", "ns",
tb.PipelineResourceSpec("cloudEvent",
tb.PipelineResourceSpecParam("targetURI", "http://sink:8080"),
),
),
}

ns := []*corev1.Namespace{
Expand Down Expand Up @@ -1051,6 +1056,12 @@ func Test_getPipelineResourceByFormat(t *testing.T) {
t.Errorf("output storage = %v, want %v", output, expected)
}

output = getOptionsByType(resFormat, "cloudEvent")
expected = []string{"scaffold-cloud (http://sink:8080)"}
if !reflect.DeepEqual(output, expected) {
t.Errorf("output storage = %v, want %v", output, expected)
}

output = getOptionsByType(resFormat, "file")
expected = []string{}
if !reflect.DeepEqual(output, expected) {
Expand Down Expand Up @@ -2227,6 +2238,101 @@ func Test_start_pipeline_clusterRes_withExistingRes_createNew(t *testing.T) {
}
}

func Test_start_pipeline_cloudEventRes_withExistingRes_createNew(t *testing.T) {

pipelineName := "cloudpipeline"

cs, _ := test.SeedTestData(t, pipelinetest.Data{
Pipelines: []*v1alpha1.Pipeline{
tb.Pipeline(pipelineName, "ns",
tb.PipelineSpec(
tb.PipelineDeclaredResource("cloudres", "cloudEvent"),
tb.PipelineTask("unit-test-1", "unit-test-task",
tb.PipelineTaskInputResource("clusres", "clusterresource"),
),
),
),
},

PipelineResources: []*v1alpha1.PipelineResource{
tb.PipelineResource("cloudresource", "ns",
tb.PipelineResourceSpec("cloudEvent",
tb.PipelineResourceSpecParam("targetURI", "https://10.20.30.40/"),
),
),
},
})

tests := []promptTest{
{
name: "newCloudResource",
cmdArgs: []string{pipelineName},

procedure: func(c *expect.Console) error {
if _, err := c.ExpectString("Choose the cloudEvent resource to use for cloudres"); err != nil {
return err
}

if _, err := c.Send(string(terminal.KeyArrowDown)); err != nil {
return err
}

if _, err := c.ExpectString("create new \"cloudEvent\" resource"); err != nil {
return err
}

if _, err := c.Send(string(terminal.KeyEnter)); err != nil {
return err
}

if _, err := c.ExpectString("Enter a name for a pipeline resource :"); err != nil {
return err
}

if _, err := c.SendLine("newcloudresource"); err != nil {
return err
}

if _, err := c.ExpectString("Enter a value for targetURI :"); err != nil {
return err
}

if _, err := c.SendLine("https://10.10.10.10"); err != nil {
return err
}

if _, err := c.Send(string(terminal.KeyEnter)); err != nil {
return err
}

if _, err := c.ExpectEOF(); err != nil {
return err
}

tekton := cs.Pipeline.Tekton()
runs, err := tekton.PipelineRuns("ns").List(v1.ListOptions{})
if err != nil {
return err
}

if runs.Items != nil && runs.Items[0].Spec.PipelineRef.Name != pipelineName {
return errors.New("pipelinerun not found")
}

c.Close()
return nil
},
},
}
opts := startOpts("ns", cs, false, "svc1", []string{"task1=svc1"})

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
opts.RunPromptTest(t, test)
})
}
}

func startOpts(ns string, cs pipelinetest.Clients, last bool, svc string, svcs []string) *startOptions {
p := test.Params{
Kube: cs.Kube,
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/pipelineresource/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ func details(pre v1alpha1.PipelineResource) string {
var key = "url"
if pre.Spec.Type == v1alpha1.PipelineResourceTypeStorage {
key = "location"
} else if pre.Spec.Type == v1alpha1.PipelineResourceTypeCloudEvent {
}
if pre.Spec.Type == v1alpha1.PipelineResourceTypeCloudEvent {
key = "targeturi"
}

Expand Down