github.phpd.cn/hashicorp/packer@v1.3.2/builder/ncloud/config_test.go (about) 1 package ncloud 2 3 import ( 4 "strings" 5 "testing" 6 ) 7 8 func testConfig() map[string]interface{} { 9 return map[string]interface{}{ 10 "access_key": "access_key", 11 "secret_key": "secret_key", 12 "server_image_product_code": "SPSW0WINNT000016", 13 "server_product_code": "SPSVRSSD00000011", 14 "server_image_name": "packer-test {{timestamp}}", 15 "server_image_description": "server description", 16 "block_storage_size": 100, 17 "user_data": "#!/bin/sh\nyum install -y httpd\ntouch /var/www/html/index.html\nchkconfig --level 2345 httpd on", 18 "region": "Korea", 19 "access_control_group_configuration_no": "33", 20 "communicator": "ssh", 21 "ssh_username": "root", 22 } 23 } 24 25 func testConfigForMemberServerImage() map[string]interface{} { 26 return map[string]interface{}{ 27 "access_key": "access_key", 28 "secret_key": "secret_key", 29 "server_product_code": "SPSVRSSD00000011", 30 "member_server_image_no": "2440", 31 "server_image_name": "packer-test {{timestamp}}", 32 "server_image_description": "server description", 33 "block_storage_size": 100, 34 "user_data": "#!/bin/sh\nyum install -y httpd\ntouch /var/www/html/index.html\nchkconfig --level 2345 httpd on", 35 "region": "Korea", 36 "access_control_group_configuration_no": "33", 37 "communicator": "ssh", 38 "ssh_username": "root", 39 } 40 } 41 42 func TestConfigWithServerImageProductCode(t *testing.T) { 43 raw := testConfig() 44 45 c, _, _ := NewConfig(raw) 46 47 if c.AccessKey != "access_key" { 48 t.Errorf("Expected 'access_key' to be set to '%s', but got '%s'.", raw["access_key"], c.AccessKey) 49 } 50 51 if c.SecretKey != "secret_key" { 52 t.Errorf("Expected 'secret_key' to be set to '%s', but got '%s'.", raw["secret_key"], c.SecretKey) 53 } 54 55 if c.ServerImageProductCode != "SPSW0WINNT000016" { 56 t.Errorf("Expected 'server_image_product_code' to be set to '%s', but got '%s'.", raw["server_image_product_code"], c.ServerImageProductCode) 57 } 58 59 if c.ServerProductCode != "SPSVRSSD00000011" { 60 t.Errorf("Expected 'server_product_code' to be set to '%s', but got '%s'.", raw["server_product_code"], c.ServerProductCode) 61 } 62 63 if c.BlockStorageSize != 100 { 64 t.Errorf("Expected 'block_storage_size' to be set to '%d', but got '%d'.", raw["block_storage_size"], c.BlockStorageSize) 65 } 66 67 if c.ServerImageDescription != "server description" { 68 t.Errorf("Expected 'server_image_description_key' to be set to '%s', but got '%s'.", raw["server_image_description"], c.ServerImageDescription) 69 } 70 71 if c.Region != "Korea" { 72 t.Errorf("Expected 'region' to be set to '%s', but got '%s'.", raw["server_image_description"], c.Region) 73 } 74 } 75 76 func TestConfigWithMemberServerImageCode(t *testing.T) { 77 raw := testConfigForMemberServerImage() 78 79 c, _, _ := NewConfig(raw) 80 81 if c.AccessKey != "access_key" { 82 t.Errorf("Expected 'access_key' to be set to '%s', but got '%s'.", raw["access_key"], c.AccessKey) 83 } 84 85 if c.SecretKey != "secret_key" { 86 t.Errorf("Expected 'secret_key' to be set to '%s', but got '%s'.", raw["secret_key"], c.SecretKey) 87 } 88 89 if c.MemberServerImageNo != "2440" { 90 t.Errorf("Expected 'member_server_image_no' to be set to '%s', but got '%s'.", raw["member_server_image_no"], c.MemberServerImageNo) 91 } 92 93 if c.ServerProductCode != "SPSVRSSD00000011" { 94 t.Errorf("Expected 'server_product_code' to be set to '%s', but got '%s'.", raw["server_product_code"], c.ServerProductCode) 95 } 96 97 if c.BlockStorageSize != 100 { 98 t.Errorf("Expected 'block_storage_size' to be set to '%d', but got '%d'.", raw["block_storage_size"], c.BlockStorageSize) 99 } 100 101 if c.ServerImageDescription != "server description" { 102 t.Errorf("Expected 'server_image_description_key' to be set to '%s', but got '%s'.", raw["server_image_description"], c.ServerImageDescription) 103 } 104 105 if c.Region != "Korea" { 106 t.Errorf("Expected 'region' to be set to '%s', but got '%s'.", raw["server_image_description"], c.Region) 107 } 108 } 109 110 func TestEmptyConfig(t *testing.T) { 111 raw := new(map[string]interface{}) 112 113 _, _, err := NewConfig(raw) 114 115 if err == nil { 116 t.Error("Expected Config to require 'access_key', 'secret_key' and some mandatory fields, but it did not") 117 } 118 119 if !strings.Contains(err.Error(), "access_key is required") { 120 t.Error("Expected Config to require 'access_key', but it did not") 121 } 122 123 if !strings.Contains(err.Error(), "secret_key is required") { 124 t.Error("Expected Config to require 'secret_key', but it did not") 125 } 126 127 if !strings.Contains(err.Error(), "server_image_product_code or member_server_image_no is required") { 128 t.Error("Expected Config to require 'server_image_product_code' or 'member_server_image_no', but it did not") 129 } 130 } 131 132 func TestExistsBothServerImageProductCodeAndMemberServerImageNoConfig(t *testing.T) { 133 raw := map[string]interface{}{ 134 "access_key": "access_key", 135 "secret_key": "secret_key", 136 "server_image_product_code": "SPSW0WINNT000016", 137 "server_product_code": "SPSVRSSD00000011", 138 "member_server_image_no": "2440", 139 } 140 141 _, _, err := NewConfig(raw) 142 143 if !strings.Contains(err.Error(), "Only one of server_image_product_code and member_server_image_no can be set") { 144 t.Error("Expected Config to require Only one of 'server_image_product_code' and 'member_server_image_no' can be set, but it did not") 145 } 146 }