terraform: EvalContext.Provider (shadow)

This commit is contained in:
Mitchell Hashimoto 2016-10-01 12:57:53 -07:00
parent ce56712473
commit ea8e7659e2
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
2 changed files with 79 additions and 1 deletions

View File

@ -199,6 +199,34 @@ func (c *shadowEvalContextShadow) InitProvider(n string) (ResourceProvider, erro
return result.Shadow, result.ResultErr return result.Shadow, result.ResultErr
} }
func (c *shadowEvalContextShadow) Provider(n string) ResourceProvider {
// Wait for the provider value
raw := c.Shared.Providers.Value(n)
if raw == nil {
c.err(fmt.Errorf(
"Unknown 'Provider' call for %q", n))
return nil
}
result, ok := raw.(*shadowEvalContextInitProvider)
if !ok {
c.err(fmt.Errorf(
"Unknown 'Provider' shadow value: %#v", raw))
return nil
}
result.Lock()
defer result.Unlock()
if !result.Init {
// Record the error but continue...
c.err(fmt.Errorf(
"Provider: provider %q requested but not initialized", n))
}
return result.Shadow
}
func (c *shadowEvalContextShadow) Diff() (*Diff, *sync.RWMutex) { func (c *shadowEvalContextShadow) Diff() (*Diff, *sync.RWMutex) {
return c.DiffValue, c.DiffLock return c.DiffValue, c.DiffLock
} }
@ -217,7 +245,6 @@ func (c *shadowEvalContextShadow) err(err error) error {
// TODO: All the functions below are EvalContext functions that must be impl. // TODO: All the functions below are EvalContext functions that must be impl.
func (c *shadowEvalContextShadow) Input() UIInput { return nil } func (c *shadowEvalContextShadow) Input() UIInput { return nil }
func (c *shadowEvalContextShadow) Provider(n string) ResourceProvider { return nil }
func (c *shadowEvalContextShadow) CloseProvider(n string) error { return nil } func (c *shadowEvalContextShadow) CloseProvider(n string) error { return nil }
func (c *shadowEvalContextShadow) ConfigureProvider(string, *ResourceConfig) error { return nil } func (c *shadowEvalContextShadow) ConfigureProvider(string, *ResourceConfig) error { return nil }
func (c *shadowEvalContextShadow) SetProviderConfig(string, *ResourceConfig) error { return nil } func (c *shadowEvalContextShadow) SetProviderConfig(string, *ResourceConfig) error { return nil }

View File

@ -106,3 +106,54 @@ func TestShadowEvalContextInitProvider_doubleInit(t *testing.T) {
t.Fatal("should error") t.Fatal("should error")
} }
} }
func TestShadowEvalContextProvider(t *testing.T) {
mock := new(MockEvalContext)
real, shadow := NewShadowEvalContext(mock)
// Args, results
name := "foo"
mockResult := new(MockResourceProvider)
// Configure the mock
mock.InitProviderProvider = mockResult
// Call the real func
real.InitProvider(name)
shadow.InitProvider(name)
// Get the provider twice
p := shadow.Provider(name)
if p == nil {
t.Fatal("should return provider")
}
if err := shadow.CloseShadow(); err != nil {
t.Fatalf("bad: %s", err)
}
}
func TestShadowEvalContextProvider_noInit(t *testing.T) {
mock := new(MockEvalContext)
real, shadow := NewShadowEvalContext(mock)
// Args, results
name := "foo"
mockResult := new(MockResourceProvider)
// Configure the mock
mock.InitProviderProvider = mockResult
// Call the real func
real.InitProvider(name)
// Get the provider w/o calling init
p := shadow.Provider(name)
if p == nil {
t.Fatal("should return provider")
}
if err := shadow.CloseShadow(); err == nil {
t.Fatal("should error")
}
}