Skip to content

Commit

Permalink
Add test that verifies token auth works and add an example in the docs
Browse files Browse the repository at this point in the history
Signed-off-by: Dom Del Nano <[email protected]>
  • Loading branch information
ddelnano committed Mar 14, 2024
1 parent e31a949 commit 0bea495
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pipeline {
agent any
environment {
XOA_URL = credentials("terraform-provider-xoa-url")
XOA_USER = credentials("terraform-provider-xoa-user")
XOA_PASSWORD = credentials("terraform-provider-xoa-password")
XOA_POOL = credentials("terraform-provider-xoa-pool")
XOA_TEMPLATE = credentials("terraform-provider-xoa-template")
Expand Down Expand Up @@ -31,6 +30,7 @@ pipeline {
stage('Test') {
steps {
lock('xoa-test-runner') {
sh 'export BYPASS_XOA_TOKEN=$(xo-cli --createToken $XOA_URL $XOA_USER $XOA_PASSWORD | tail -n1)'
sh 'cp /opt/terraform-provider-xenorchestra/testdata/images/alpine-virt-3.17.0-x86_64.iso xoa/testdata/alpine-virt-3.17.0-x86_64.iso'
sh 'TF_VERSION=${TF_VERSION} TIMEOUT=60m make ci'
}
Expand Down
8 changes: 8 additions & 0 deletions examples/provider/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ terraform {
xenorchestra = {
source = "vatesfr/xenorchestra"
}
xenorchestra_token_auth = {
source = "vatesfr/xenorchestra"
}
}
}

Expand All @@ -21,3 +24,8 @@ provider "xenorchestra" {
# used sparingly!
insecure = <false|true> # Or set XOA_INSECURE environment variable to any value
}

provider "xenorchestra_token_auth" {
# XOA_USER and XOA_PASSWORD cannot be set, nor can their arguments
token = "<token from XO>" # or set XOA_TOKEN environment variable
}
1 change: 1 addition & 0 deletions xoa/acc_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

var testObjectIndex int = 1
var accTestPrefix string = "terraform-acc"
var accTestXoToken string = os.Getenv("BYPASS_XOA_TOKEN")
var accTestPool client.Pool
var accTestHost client.Host
var accDefaultSr client.StorageRepository
Expand Down
22 changes: 22 additions & 0 deletions xoa/data_source_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@ func TestAccXenorchestraDataSource_host(t *testing.T) {
)
}

func TestAccXenorchestraDataSource_hostXoTokenAuth(t *testing.T) {
resourceName := "data.xenorchestra_host.host"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccTokenAuthProviders,
Steps: []resource.TestStep{
{
Config: testAccXenorchestraDataSourceHostConfig(accTestHost.NameLabel),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckXenorchestraDataSourceHost(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "id"),
resource.TestCheckResourceAttrSet(resourceName, "cpus.cores"),
resource.TestCheckResourceAttrSet(resourceName, "cpus.sockets"),
resource.TestCheckResourceAttrSet(resourceName, "memory"),
resource.TestCheckResourceAttrSet(resourceName, "memory_usage"),
resource.TestCheckResourceAttr(resourceName, "name_label", accTestHost.NameLabel)),
},
},
},
)
}

func TestAccXenorchestraDataSource_hostNotFound(t *testing.T) {
resourceName := "data.xenorchestra_host.host"
resource.Test(t, resource.TestCase{
Expand Down
26 changes: 24 additions & 2 deletions xoa/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (
)

var testAccProviders map[string]*schema.Provider
var testAccTokenAuthProviders map[string]*schema.Provider
var testAccFailToStartAndHaltProviders map[string]*schema.Provider
var testAccFailToDeleteVmProviders map[string]*schema.Provider

var testAccProvider *schema.Provider
var testAccTokenAuthProvider *schema.Provider
var testAccFailToStartHaltVmProvider *schema.Provider
var testAccFailToDeleteVmProvider *schema.Provider

Expand All @@ -22,6 +24,11 @@ func init() {
"xenorchestra": testAccProvider,
}

testAccTokenAuthProvider = Provider()
testAccTokenAuthProviders = map[string]*schema.Provider{
"xenorchestra": testAccTokenAuthProvider,
}

testAccFailToStartHaltVmProvider = Provider()
testAccFailToStartHaltVmProvider.ConfigureFunc = internal.GetFailToStartAndHaltXOClient
testAccFailToStartAndHaltProviders = map[string]*schema.Provider{
Expand All @@ -34,17 +41,32 @@ func init() {
}
}

func createTokenAuthProvider() *schema.Provider {
provider := Provider()

var f schema.SchemaDefaultFunc = func() (interface{}, error) { return "", nil }
provider.Schema["username"].DefaultFunc = f
provider.Schema["username"].ConflictsWith = []string{}

provider.Schema["password"].DefaultFunc = f
provider.Schema["password"].ConflictsWith = []string{}

provider.Schema["token"].ConflictsWith = []string{}
provider.Schema["token"].DefaultFunc = schema.EnvDefaultFunc("BYPASS_XOA_TOKEN", nil)
return provider
}

func testAccPreCheck(t *testing.T) {
if v := os.Getenv("XOA_URL"); v == "" {
t.Fatal("The XOA_URL environment variable must be set")
}

user := os.Getenv("XOA_USER")
password := os.Getenv("XOA_PASSWORD")
token := os.Getenv("XOA_TOKEN")
token := os.Getenv("BYPASS_XOA_TOKEN")

if token == "" && (user == "" || password == "") {
t.Fatal("One of the following environment variable(s) must be set: XOA_USER and XOA_PASSWORD or XOA_TOKEN")
t.Fatal("One of the following environment variable(s) must be set: XOA_USER and XOA_PASSWORD or BYPASS_XOA_TOKEN")
}

if v := os.Getenv("XOA_POOL"); v == "" {
Expand Down

0 comments on commit 0bea495

Please sign in to comment.