github.com/Microsoft/fabrikate@v0.0.0-20190420002442-bff75be28d02/core/component_test.go (about) 1 package core 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func TestRelativePathToGitComponent(t *testing.T) { 10 subcomponent := Component{ 11 Name: "efk", 12 Method: "git", 13 Source: "https://github.com/Microsoft/fabrikate-elasticsearch-fluentd-kibana", 14 } 15 16 assert.Equal(t, subcomponent.RelativePathTo(), "components/efk") 17 } 18 19 func TestRelativePathToDirectoryComponent(t *testing.T) { 20 subcomponent := Component{ 21 Name: "infra", 22 Source: "./infra", 23 } 24 25 assert.Equal(t, subcomponent.RelativePathTo(), "infra") 26 } 27 28 func TestLoadComponent(t *testing.T) { 29 component := Component{ 30 PhysicalPath: "../test/fixtures/definition/infra", 31 LogicalPath: "infra", 32 } 33 34 component, err := component.LoadComponent() 35 assert.Nil(t, err) 36 37 assert.Nil(t, err) 38 assert.Equal(t, component.Name, "infra") 39 assert.Equal(t, len(component.Subcomponents), 1) 40 assert.Equal(t, component.Subcomponents[0].Name, "efk") 41 assert.Equal(t, component.Subcomponents[0].Source, "https://github.com/Microsoft/fabrikate-elasticsearch-fluentd-kibana") 42 assert.Equal(t, component.Subcomponents[0].Method, "git") 43 } 44 45 func TestLoadConfig(t *testing.T) { 46 component := Component{ 47 PhysicalPath: "../test/fixtures/generate/infra", 48 LogicalPath: "infra", 49 } 50 51 component, err := component.LoadComponent() 52 assert.Nil(t, err) 53 54 err = component.LoadConfig([]string{"prod-east", "prod"}) 55 56 assert.Nil(t, err) 57 } 58 59 func TestIteratingDefinition(t *testing.T) { 60 callbackCount := 0 61 results := WalkComponentTree("../test/fixtures/iterator", []string{""}, func(path string, component *Component) (err error) { 62 callbackCount++ 63 return nil 64 }) 65 66 var err error 67 components := make([]Component, 0) 68 for result := range results { 69 if result.Error != nil { 70 err = result.Error 71 } else if result.Component != nil { 72 components = append(components, *result.Component) 73 } 74 } 75 76 assert.Nil(t, err) 77 assert.Equal(t, 3, len(components)) 78 assert.Equal(t, callbackCount, len(components)) 79 80 assert.Equal(t, components[1].PhysicalPath, "../test/fixtures/iterator/infra") 81 assert.Equal(t, components[1].LogicalPath, "infra") 82 83 assert.Equal(t, components[2].PhysicalPath, "../test/fixtures/iterator/infra/components/efk") 84 assert.Equal(t, components[2].LogicalPath, "infra/efk") 85 } 86 87 func TestWriteComponent(t *testing.T) { 88 component := Component{ 89 PhysicalPath: "../test/fixtures/install", 90 LogicalPath: "", 91 } 92 93 component, err := component.LoadComponent() 94 assert.Nil(t, err) 95 96 err = component.Write() 97 assert.Nil(t, err) 98 }