From fb60b6f6f293697c4ce12974251b4146b03362ea Mon Sep 17 00:00:00 2001 From: James Bardin Date: Fri, 3 Feb 2017 15:31:21 -0500 Subject: [PATCH] Add separate program for locking state files Depending on the implementation, local state locks may be reentrant within the same process. Use a separate process to test locked state files. --- command/testdata/statelocker.go | 43 +++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 command/testdata/statelocker.go diff --git a/command/testdata/statelocker.go b/command/testdata/statelocker.go new file mode 100644 index 000000000..a70e3d6da --- /dev/null +++ b/command/testdata/statelocker.go @@ -0,0 +1,43 @@ +// statelocker use used for testing command with a locked state. +// This will lock the state file at a given path, then wait for a sigal. On +// SIGINT and SIGTERM the state will be Unlocked before exit. +package main + +import ( + "io" + "log" + "os" + "os/signal" + "syscall" + + "github.com/hashicorp/terraform/state" +) + +func main() { + if len(os.Args) != 2 { + log.Fatal(os.Args[0], "statefile") + } + + s := &state.LocalState{ + Path: os.Args[1], + } + + err := s.Lock("command test") + if err != nil { + io.WriteString(os.Stderr, err.Error()) + return + } + + // signal to the caller that we're locked + io.WriteString(os.Stdout, "LOCKED") + + defer func() { + if err := s.Unlock(); err != nil { + io.WriteString(os.Stderr, err.Error()) + } + }() + + c := make(chan os.Signal, 1) + signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) + <-c +}