From a4503eb4d84773afdcd8eb6ef05b94c779f4d74b Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Thu, 20 Dec 2018 10:36:49 -0500 Subject: [PATCH] Add data source tests for file --- data_source_quantum_file_test.go | 59 ++++++++++++++++++++++++++++++++ provider.go | 26 +++++++------- provider_test.go | 18 ++++++++++ 3 files changed, 91 insertions(+), 12 deletions(-) create mode 100644 data_source_quantum_file_test.go create mode 100644 provider_test.go diff --git a/data_source_quantum_file_test.go b/data_source_quantum_file_test.go new file mode 100644 index 0000000..adcdb1d --- /dev/null +++ b/data_source_quantum_file_test.go @@ -0,0 +1,59 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + "testing" + + r "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestFile_basic(t *testing.T) { + var cases = []struct { + path string + content string + config string + }{ + { + "quantum_file", + "This is some content", + `data "quantum_file" "file" { + content = "This is some content" + filename = "quantum_file" + }`, + }, + { + "quantum_file", + "This is some sensitive content", + `data "quantum_file" "file" { + sensitive_content = "This is some sensitive content" + filename = "quantum_file" + }`, + }, + } + + for _, tt := range cases { + r.UnitTest(t, r.TestCase{ + Providers: testProviders, + Steps: []r.TestStep{ + { + Config: tt.config, + Check: func(s *terraform.State) error { + content, err := ioutil.ReadFile(tt.path) + if err != nil { + return fmt.Errorf("config:\n%s\n,got: %s\n", tt.config, err) + } + if string(content) != tt.content { + return fmt.Errorf("config:\n%s\ngot:\n%s\nwant:\n%s\n", tt.config, content, tt.content) + } + return nil + }, + }, + }, + }) + } + + os.Remove("quantum_file") +} diff --git a/provider.go b/provider.go index 26817ad..b8e8629 100644 --- a/provider.go +++ b/provider.go @@ -6,19 +6,21 @@ import ( "github.com/hashicorp/terraform/terraform" ) +func Provider() terraform.ResourceProvider { + return &schema.Provider{ + DataSourcesMap: map[string]*schema.Resource{ + "quantum_file": dataSourceQuantumFile(), + "quantum_query_json": dataSourceQuantumQueryJSON(), + "quantum_list_files": dataSourceQuantumListFiles(), + }, + ResourcesMap: map[string]*schema.Resource{ + "quantum_password": resourceQuantumPassword(), + }, + } +} + func main() { plugin.Serve(&plugin.ServeOpts{ - ProviderFunc: func() terraform.ResourceProvider { - return &schema.Provider{ - DataSourcesMap: map[string]*schema.Resource{ - "quantum_file": dataSourceQuantumFile(), - "quantum_query_json": dataSourceQuantumQueryJSON(), - "quantum_list_files": dataSourceQuantumListFiles(), - }, - ResourcesMap: map[string]*schema.Resource{ - "quantum_password": resourceQuantumPassword(), - }, - } - }, + ProviderFunc: Provider, }) } diff --git a/provider_test.go b/provider_test.go new file mode 100644 index 0000000..b5ab89f --- /dev/null +++ b/provider_test.go @@ -0,0 +1,18 @@ +package main + +import ( + "testing" + + "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/terraform" +) + +var testProviders = map[string]terraform.ResourceProvider{ + "quantum": Provider(), +} + +func TestProvider(t *testing.T) { + if err := Provider().(*schema.Provider).InternalValidate(); err != nil { + t.Fatalf("err: %s", err) + } +}