github.com/raghuse92/packer@v1.3.2/builder/openstack/run_config_test.go (about) 1 package openstack 2 3 import ( 4 "os" 5 "testing" 6 7 "github.com/gophercloud/gophercloud/openstack/imageservice/v2/images" 8 "github.com/hashicorp/packer/helper/communicator" 9 "github.com/mitchellh/mapstructure" 10 ) 11 12 func init() { 13 // Clear out the openstack env vars so they don't 14 // affect our tests. 15 os.Setenv("SDK_USERNAME", "") 16 os.Setenv("SDK_PASSWORD", "") 17 os.Setenv("SDK_PROVIDER", "") 18 } 19 20 func testRunConfig() *RunConfig { 21 return &RunConfig{ 22 SourceImage: "abcd", 23 Flavor: "m1.small", 24 25 Comm: communicator.Config{ 26 SSHUsername: "foo", 27 }, 28 } 29 } 30 31 func TestRunConfigPrepare(t *testing.T) { 32 c := testRunConfig() 33 err := c.Prepare(nil) 34 if len(err) > 0 { 35 t.Fatalf("err: %s", err) 36 } 37 } 38 39 func TestRunConfigPrepare_InstanceType(t *testing.T) { 40 c := testRunConfig() 41 c.Flavor = "" 42 if err := c.Prepare(nil); len(err) != 1 { 43 t.Fatalf("err: %s", err) 44 } 45 } 46 47 func TestRunConfigPrepare_SourceImage(t *testing.T) { 48 c := testRunConfig() 49 c.SourceImage = "" 50 if err := c.Prepare(nil); len(err) != 1 { 51 t.Fatalf("err: %s", err) 52 } 53 } 54 55 func TestRunConfigPrepare_SSHPort(t *testing.T) { 56 c := testRunConfig() 57 c.Comm.SSHPort = 0 58 if err := c.Prepare(nil); len(err) != 0 { 59 t.Fatalf("err: %s", err) 60 } 61 62 if c.Comm.SSHPort != 22 { 63 t.Fatalf("invalid value: %d", c.Comm.SSHPort) 64 } 65 66 c.Comm.SSHPort = 44 67 if err := c.Prepare(nil); len(err) != 0 { 68 t.Fatalf("err: %s", err) 69 } 70 71 if c.Comm.SSHPort != 44 { 72 t.Fatalf("invalid value: %d", c.Comm.SSHPort) 73 } 74 } 75 76 func TestRunConfigPrepare_BlockStorage(t *testing.T) { 77 c := testRunConfig() 78 c.UseBlockStorageVolume = true 79 c.VolumeType = "fast" 80 if err := c.Prepare(nil); len(err) != 0 { 81 t.Fatalf("err: %s", err) 82 } 83 if c.VolumeType != "fast" { 84 t.Fatalf("invalid value: %s", c.VolumeType) 85 } 86 87 c.AvailabilityZone = "RegionTwo" 88 if err := c.Prepare(nil); len(err) != 0 { 89 t.Fatalf("err: %s", err) 90 } 91 92 if c.VolumeAvailabilityZone != "RegionTwo" { 93 t.Fatalf("invalid value: %s", c.VolumeAvailabilityZone) 94 } 95 96 c.VolumeAvailabilityZone = "RegionOne" 97 if err := c.Prepare(nil); len(err) != 0 { 98 t.Fatalf("err: %s", err) 99 } 100 101 if c.VolumeAvailabilityZone != "RegionOne" { 102 t.Fatalf("invalid value: %s", c.VolumeAvailabilityZone) 103 } 104 105 c.VolumeName = "PackerVolume" 106 if c.VolumeName != "PackerVolume" { 107 t.Fatalf("invalid value: %s", c.VolumeName) 108 } 109 } 110 111 func TestRunConfigPrepare_FloatingIPPoolCompat(t *testing.T) { 112 c := testRunConfig() 113 c.FloatingIPPool = "uuid1" 114 if err := c.Prepare(nil); len(err) != 0 { 115 t.Fatalf("err: %s", err) 116 } 117 118 if c.FloatingIPNetwork != "uuid1" { 119 t.Fatalf("invalid value: %s", c.FloatingIPNetwork) 120 } 121 122 c.FloatingIPNetwork = "uuid2" 123 c.FloatingIPPool = "uuid3" 124 if err := c.Prepare(nil); len(err) != 0 { 125 t.Fatalf("err: %s", err) 126 } 127 128 if c.FloatingIPNetwork != "uuid2" { 129 t.Fatalf("invalid value: %s", c.FloatingIPNetwork) 130 } 131 } 132 133 // This test case confirms that only allowed fields will be set to values 134 // The checked values are non-nil for their target type 135 func TestBuildImageFilter(t *testing.T) { 136 137 filters := ImageFilterOptions{ 138 Name: "Ubuntu 16.04", 139 Visibility: "public", 140 Owner: "1234567890", 141 Tags: []string{"prod", "ready"}, 142 } 143 144 listOpts, err := filters.Build() 145 if err != nil { 146 t.Errorf("Building filter failed with: %s", err) 147 } 148 149 if listOpts.Name != "Ubuntu 16.04" { 150 t.Errorf("Name did not build correctly: %s", listOpts.Name) 151 } 152 153 if listOpts.Visibility != images.ImageVisibilityPublic { 154 t.Errorf("Visibility did not build correctly: %s", listOpts.Visibility) 155 } 156 157 if listOpts.Owner != "1234567890" { 158 t.Errorf("Owner did not build correctly: %s", listOpts.Owner) 159 } 160 } 161 162 func TestBuildBadImageFilter(t *testing.T) { 163 filterMap := map[string]interface{}{ 164 "limit": "3", 165 "size_min": "25", 166 } 167 168 filters := ImageFilterOptions{} 169 mapstructure.Decode(filterMap, &filters) 170 listOpts, err := filters.Build() 171 172 if err != nil { 173 t.Errorf("Error returned processing image filter: %s", err.Error()) 174 return // we cannot trust listOpts to not cause unexpected behaviour 175 } 176 177 if listOpts.Limit == filterMap["limit"] { 178 t.Errorf("Limit was parsed into ListOpts: %d", listOpts.Limit) 179 } 180 181 if listOpts.SizeMin != 0 { 182 t.Errorf("SizeMin was parsed into ListOpts: %d", listOpts.SizeMin) 183 } 184 185 if listOpts.Sort != "created_at:desc" { 186 t.Errorf("Sort was not applied: %s", listOpts.Sort) 187 } 188 189 if !filters.Empty() { 190 t.Errorf("The filters should be empty due to lack of input") 191 } 192 } 193 194 // Tests that the Empty method on ImageFilterOptions works as expected 195 func TestImageFiltersEmpty(t *testing.T) { 196 filledFilters := ImageFilterOptions{ 197 Name: "Ubuntu 16.04", 198 Visibility: "public", 199 Owner: "1234567890", 200 Tags: []string{"prod", "ready"}, 201 } 202 203 if filledFilters.Empty() { 204 t.Errorf("Expected filled filters to be non-empty: %v", filledFilters) 205 } 206 207 emptyFilters := ImageFilterOptions{} 208 209 if !emptyFilters.Empty() { 210 t.Errorf("Expected default filter to be empty: %v", emptyFilters) 211 } 212 }