Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ims
terraform-provider-one
Commits
1dcf214f
Commit
1dcf214f
authored
Dec 04, 2017
by
Yann KAISER
Browse files
Updated the provider_test file
parent
a65875b0
Changes
1
Hide whitespace changes
Inline
Side-by-side
one/provider_test.go
View file @
1dcf214f
...
...
@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"os/user"
"testing"
...
...
@@ -12,61 +13,106 @@ import (
"github.com/hashicorp/terraform/terraform"
)
type
ConfigStruct
struct
{
endpoint
string
`JSON:"endpoint"`
username
string
`JSON:"username"`
password
string
`JSON:"password"`
oneflowHost
string
`JSON:"oneflow_host"`
vmTimeout
int
`JSON:"vm_timeout"`
type
TestConfigConnection
struct
{
Endpoint
string
`json:"endpoint,omitempty"`
Username
string
`json:"username,omitempty"`
Password
string
`json:"password,omitempty"`
OneflowHost
string
`json:"oneflow_host,omitempty"`
VMTimeout
int
`json:"vm_timeout,omitempty"`
}
type
TestConfigTests
struct
{
ResourceVM
map
[
string
]
interface
{}
`json:"resource_vm"`
ResourceDisk
map
[
string
]
interface
{}
`json:"resource_disk"`
ResourceImage
map
[
string
]
interface
{}
`json:"resource_image"`
ResourceVNetwork
map
[
string
]
interface
{}
`json:"resource_vnetwork"`
ResourceVMTemplate
map
[
string
]
interface
{}
`json:"resource_vm_template"`
ResourceService
map
[
string
]
interface
{}
`json:"resource_service"`
DataImage
map
[
string
]
interface
{}
`json:"data_image"`
DataDatastore
map
[
string
]
interface
{}
`json:"data_datastore"`
DataHost
map
[
string
]
interface
{}
`json:"data_host"`
DataService
map
[
string
]
interface
{}
`json:"data_service"`
DataUser
map
[
string
]
interface
{}
`json:"data_user"`
DataVMTemplate
map
[
string
]
interface
{}
`json:"data_vm_template"`
DataVNetwork
map
[
string
]
interface
{}
`json:"data_vnetwork"`
}
type
TestConfig
struct
{
Connection
TestConfigConnection
`json:"connection"`
Tests
TestConfigTests
`json:"tests"`
}
var
testAccProviders
map
[
string
]
terraform
.
ResourceProvider
var
testAccProvider
*
schema
.
Provider
var
testContext
TestConfigTests
var
testClient
*
Client
func
init
()
{
testAccProvider
=
Provider
()
testAccProviders
=
map
[
string
]
terraform
.
ResourceProvider
{
"one"
:
testAccProvider
,
}
}
func
testAccPreCheck
(
t
*
testing
.
T
)
{
usr
,
_
:=
user
.
Current
()
if
_
,
err
:=
os
.
Stat
(
usr
.
HomeDir
+
"/.one/one_auth"
);
os
.
IsNotExist
(
err
)
{
t
.
Fatal
(
"You must create the auth file at ~/.one/one_auth to authenticate"
)
// Setup testContext
err
:=
SetupTestContext
()
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
}
func
CreateConfigFromJSON
(
jsonFilePath
string
)
(
*
Config
,
error
)
{
bytes
,
err
:=
ioutil
.
ReadFile
(
jsonFilePath
)
func
SetupTestContext
()
error
{
if
_
,
err
:=
os
.
Stat
(
"./testinfo.json"
);
os
.
IsNotExist
(
err
)
{
log
.
Fatal
(
"You must have a testinfo.json file in the 'one' folder."
)
}
bytes
,
err
:=
ioutil
.
ReadFile
(
"./testinfo.json"
)
if
err
!=
nil
{
return
nil
,
err
return
err
}
var
conf
map
[
string
]
interface
{}
credentials
,
err
:=
GetCredentials
()
var
testInfos
TestConfig
if
err
=
json
.
Unmarshal
(
bytes
,
&
testInfos
);
err
!=
nil
{
return
fmt
.
Errorf
(
"error while reading the testinfo.json file: %s"
,
err
)
}
if
err
:=
json
.
Unmarshal
(
bytes
,
&
conf
);
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"error while reading the file"
)
testContext
=
testInfos
.
Tests
var
testConfigConnection
=
testInfos
.
Connection
testClient
,
err
=
GenerateClientInfos
(
testConfigConnection
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"%s"
,
err
)
}
vmTimeout
:=
conf
[
"vm_timeout"
]
.
(
float64
)
return
nil
}
var
createdConfig
=
CreateConfig
(
conf
[
"endpoint"
]
.
(
string
),
credentials
[
0
],
credentials
[
1
],
conf
[
"oneflow_host"
]
.
(
string
),
int
(
vmTimeout
)
)
func
testAccPreCheck
(
t
*
testing
.
T
)
{
usr
,
_
:=
user
.
Current
(
)
return
&
createdConfig
,
nil
if
_
,
err
:=
os
.
Stat
(
usr
.
HomeDir
+
"/.one/one_auth"
);
os
.
IsNotExist
(
err
)
{
t
.
Fatal
(
"You must create the auth file at ~/.one/one_auth to authenticate"
)
}
err
:=
testAccProvider
.
Configure
(
terraform
.
NewResourceConfig
(
nil
))
if
err
!=
nil
{
log
.
Print
(
"ERROR!"
)
t
.
Fatal
(
err
)
}
}
func
GenerateClientInfos
(
jsonFilePath
string
)
(
*
Client
,
error
)
{
c
onfig
,
err
:=
CreateConfigFromJSON
(
jsonFilePath
)
func
GenerateClientInfos
(
connectionInfos
TestConfigConnection
)
(
*
Client
,
error
)
{
c
redentials
,
err
:=
GetCredentials
(
)
if
err
!=
nil
{
credentials
[
0
],
credentials
[
1
]
=
connectionInfos
.
Username
,
connectionInfos
.
Password
return
nil
,
err
}
config
:=
CreateConfig
(
connectionInfos
.
Endpoint
,
credentials
[
0
],
credentials
[
1
],
connectionInfos
.
OneflowHost
,
connectionInfos
.
VMTimeout
)
clientItf
,
err
:=
config
.
Client
()
if
err
!=
nil
{
return
nil
,
err
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment