-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
183eadf
commit 236800d
Showing
3 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//go:build integration | ||
|
||
package testintegration_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/canonical/pebble/internals/testintegration" | ||
) | ||
|
||
func TestPebbleSomethingElse(t *testing.T) { | ||
pebbleDir := t.TempDir() | ||
CreateLayer(t, pebbleDir, "001-simple-layer.yaml", DefaultLayerYAML) | ||
_ = PebbleRun(t, pebbleDir) | ||
// do something | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//go:build integration | ||
|
||
package testintegration_test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
. "github.com/canonical/pebble/internals/testintegration" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
if err := Setup(); err != nil { | ||
fmt.Println("Setup failed with error:", err) | ||
os.Exit(1) | ||
} | ||
|
||
exitVal := m.Run() | ||
os.Exit(exitVal) | ||
} | ||
|
||
func TestPebbleRunWithSimpleLayer(t *testing.T) { | ||
pebbleDir := t.TempDir() | ||
|
||
layerYAML := ` | ||
services: | ||
demo-service: | ||
override: replace | ||
command: sleep 1000 | ||
startup: enabled | ||
`[1:] | ||
CreateLayer(t, pebbleDir, "001-simple-layer.yaml", layerYAML) | ||
|
||
logs := PebbleRun(t, pebbleDir) | ||
|
||
expected := []string{ | ||
"Service \"demo-service\" starting", | ||
"Started default services with change", | ||
} | ||
|
||
if foundAll, notFound := AllExpectedKeywordsFoundInLogs(logs, expected); !foundAll { | ||
t.Errorf("Expected keywords not found in logs: %v", notFound) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
//go:build integration | ||
|
||
package testintegration | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
"time" | ||
) | ||
|
||
var DefaultLayerYAML string = ` | ||
services: | ||
demo-service: | ||
override: replace | ||
command: sleep 1000 | ||
startup: enabled | ||
`[1:] | ||
|
||
func Setup() error { | ||
cmd := exec.Command("go", "build", "./cmd/pebble") | ||
cmd.Dir = getRootDir() | ||
return cmd.Run() | ||
} | ||
|
||
func getRootDir() string { | ||
wd, _ := os.Getwd() | ||
return filepath.Join(wd, "../../") | ||
} | ||
|
||
func AllExpectedKeywordsFoundInLogs(logs []string, keywords []string) (bool, []string) { | ||
var notFound []string | ||
|
||
for _, keyword := range keywords { | ||
keywordFound := false | ||
for _, log := range logs { | ||
if strings.Contains(log, keyword) { | ||
keywordFound = true | ||
break | ||
} | ||
} | ||
if !keywordFound { | ||
notFound = append(notFound, keyword) | ||
} | ||
} | ||
|
||
return len(notFound) == 0, notFound | ||
} | ||
|
||
func CreateLayer(t *testing.T, pebbleDir string, layerFileName string, layerYAML string) { | ||
layersDir := filepath.Join(pebbleDir, "layers") | ||
err := os.MkdirAll(layersDir, 0755) | ||
if err != nil { | ||
t.Fatalf("Error creating layers directory: pipe: %v", err) | ||
} | ||
|
||
layerPath := filepath.Join(layersDir, layerFileName) | ||
err = os.WriteFile(layerPath, []byte(layerYAML), 0755) | ||
if err != nil { | ||
t.Fatalf("Error creating layers file: %v", err) | ||
} | ||
} | ||
|
||
func PebbleRun(t *testing.T, pebbleDir string) []string { | ||
cmd := exec.Command("./pebble", "run") | ||
cmd.Dir = getRootDir() | ||
cmd.Env = append(os.Environ(), fmt.Sprintf("PEBBLE=%s", pebbleDir)) | ||
|
||
stderrPipe, err := cmd.StderrPipe() | ||
if err != nil { | ||
t.Fatalf("Error creating stderr pipe: %v", err) | ||
} | ||
|
||
err = cmd.Start() | ||
defer cmd.Process.Kill() | ||
if err != nil { | ||
t.Fatalf("Error starting 'pebble run': %v", err) | ||
} | ||
|
||
var logs []string | ||
|
||
lastOutputTime := time.Now() | ||
|
||
go func() { | ||
scanner := bufio.NewScanner(stderrPipe) | ||
for scanner.Scan() { | ||
lastOutputTime = time.Now() | ||
line := scanner.Text() | ||
logs = append(logs, line) | ||
} | ||
}() | ||
|
||
for { | ||
time.Sleep(100 * time.Millisecond) | ||
if time.Since(lastOutputTime) > 1*time.Second { | ||
break | ||
} | ||
} | ||
|
||
return logs | ||
} |