github.com/richardmarshall/terraform@v0.9.5-0.20170429023105-15704cc6ee35/helper/schema/backend_test.go (about)

     1  package schema
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/config"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestBackendValidate(t *testing.T) {
    13  	cases := []struct {
    14  		Name   string
    15  		B      *Backend
    16  		Config map[string]interface{}
    17  		Err    bool
    18  	}{
    19  		{
    20  			"Basic required field",
    21  			&Backend{
    22  				Schema: map[string]*Schema{
    23  					"foo": &Schema{
    24  						Required: true,
    25  						Type:     TypeString,
    26  					},
    27  				},
    28  			},
    29  			nil,
    30  			true,
    31  		},
    32  
    33  		{
    34  			"Basic required field set",
    35  			&Backend{
    36  				Schema: map[string]*Schema{
    37  					"foo": &Schema{
    38  						Required: true,
    39  						Type:     TypeString,
    40  					},
    41  				},
    42  			},
    43  			map[string]interface{}{
    44  				"foo": "bar",
    45  			},
    46  			false,
    47  		},
    48  	}
    49  
    50  	for i, tc := range cases {
    51  		t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) {
    52  			c, err := config.NewRawConfig(tc.Config)
    53  			if err != nil {
    54  				t.Fatalf("err: %s", err)
    55  			}
    56  
    57  			_, es := tc.B.Validate(terraform.NewResourceConfig(c))
    58  			if len(es) > 0 != tc.Err {
    59  				t.Fatalf("%d: %#v", i, es)
    60  			}
    61  		})
    62  	}
    63  }
    64  
    65  func TestBackendConfigure(t *testing.T) {
    66  	cases := []struct {
    67  		Name   string
    68  		B      *Backend
    69  		Config map[string]interface{}
    70  		Err    bool
    71  	}{
    72  		{
    73  			"Basic config",
    74  			&Backend{
    75  				Schema: map[string]*Schema{
    76  					"foo": &Schema{
    77  						Type:     TypeInt,
    78  						Optional: true,
    79  					},
    80  				},
    81  
    82  				ConfigureFunc: func(ctx context.Context) error {
    83  					d := FromContextBackendConfig(ctx)
    84  					if d.Get("foo").(int) != 42 {
    85  						return fmt.Errorf("bad config data")
    86  					}
    87  
    88  					return nil
    89  				},
    90  			},
    91  			map[string]interface{}{
    92  				"foo": 42,
    93  			},
    94  			false,
    95  		},
    96  	}
    97  
    98  	for i, tc := range cases {
    99  		t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) {
   100  			c, err := config.NewRawConfig(tc.Config)
   101  			if err != nil {
   102  				t.Fatalf("err: %s", err)
   103  			}
   104  
   105  			err = tc.B.Configure(terraform.NewResourceConfig(c))
   106  			if err != nil != tc.Err {
   107  				t.Fatalf("%d: %s", i, err)
   108  			}
   109  		})
   110  	}
   111  }