github.com/joshdk/godel@v0.0.0-20170529232908-862138a45aee/apps/distgo/cmd/docker/order_specs_test.go (about) 1 // Copyright 2016 Palantir Technologies, 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 docker_test 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/assert" 21 "github.com/stretchr/testify/require" 22 23 "github.com/palantir/godel/apps/distgo/cmd/docker" 24 "github.com/palantir/godel/apps/distgo/params" 25 ) 26 27 func generateSpec(product string, deps params.DockerDeps) params.ProductBuildSpec { 28 return params.ProductBuildSpec{ 29 ProductName: product, 30 Product: params.Product{ 31 DockerImages: []params.DockerImage{ 32 { 33 Deps: deps, 34 }, 35 }, 36 }, 37 } 38 } 39 40 func TestOrderBuildSpecs(t *testing.T) { 41 A := generateSpec("A", params.DockerDeps{ 42 {Product: "B", Type: params.DockerDepDocker, TargetFile: ""}, 43 {Product: "C", Type: params.DockerDepDocker, TargetFile: ""}, 44 }) 45 B := generateSpec("B", params.DockerDeps{ 46 {Product: "D", Type: params.DockerDepDocker, TargetFile: ""}, 47 }) 48 C := generateSpec("C", params.DockerDeps{ 49 {Product: "D", Type: params.DockerDepDocker, TargetFile: ""}, 50 }) 51 D := generateSpec("D", params.DockerDeps{}) 52 E := generateSpec("E", params.DockerDeps{ 53 {Product: "DepE", Type: params.DockerDepDocker, TargetFile: ""}, 54 }) 55 DepE := generateSpec("DepE", params.DockerDeps{ 56 {Product: "E", Type: params.DockerDepDocker, TargetFile: ""}, 57 }) 58 59 X := generateSpec("X", params.DockerDeps{ 60 {Product: "Y", Type: params.DockerDepDocker, TargetFile: ""}, 61 }) 62 Y := generateSpec("Y", params.DockerDeps{ 63 {Product: "Z", Type: params.DockerDepDocker, TargetFile: ""}, 64 }) 65 Z := generateSpec("Z", params.DockerDeps{}) 66 67 for _, testcase := range []struct { 68 input []params.ProductBuildSpecWithDeps 69 expected []params.ProductBuildSpecWithDeps 70 expectErr string 71 }{ 72 { 73 // (A <- B,C <- D) = D, B, C, A 74 input: []params.ProductBuildSpecWithDeps{{Spec: A}, {Spec: B}, {Spec: C}, {Spec: D}}, 75 expected: []params.ProductBuildSpecWithDeps{{Spec: D}, {Spec: B}, {Spec: C}, {Spec: A}}, 76 }, 77 { 78 // empty 79 input: []params.ProductBuildSpecWithDeps{}, 80 expected: []params.ProductBuildSpecWithDeps{}, 81 }, 82 { 83 // (E <- DepE <- E) = invalid 84 input: []params.ProductBuildSpecWithDeps{{Spec: E}, {Spec: DepE}}, 85 expected: nil, 86 expectErr: "Failed to generate ordering among the products.", 87 }, 88 { 89 // (X <- Y <- Z) = Z, Y, X 90 input: []params.ProductBuildSpecWithDeps{{Spec: Y}, {Spec: X}, {Spec: Z}}, 91 expected: []params.ProductBuildSpecWithDeps{{Spec: Z}, {Spec: Y}, {Spec: X}}, 92 }, 93 } { 94 actual, err := docker.OrderBuildSpecs(testcase.input) 95 if testcase.expectErr != "" { 96 require.Contains(t, err.Error(), testcase.expectErr) 97 continue 98 } 99 require.NoError(t, err) 100 for i, expectedSpec := range testcase.expected { 101 assert.Equal(t, expectedSpec.Spec.ProductName, actual[i].Spec.ProductName) 102 } 103 } 104 }