2021-04-27 01:18:45 +00:00
|
|
|
// Copyright 2016-2021, Pulumi Corporation.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package auto
|
|
|
|
|
|
|
|
import (
|
2022-04-22 16:50:40 +00:00
|
|
|
"context"
|
|
|
|
"os"
|
2021-04-27 01:18:45 +00:00
|
|
|
"testing"
|
automation: only read complete lines before trying to deserialize (#15778)
When tailing the event log in automation API we currently have nothing
that makes sure we read only complete lines. This means if the OS
happens to flush an incomplete line for whatever reason (or the Go JSON
encoder does, which we're using to write these lines), we might read a
line that is incompletely written, and thus will fail to JSON decode it.
Since the JSON encoder always writes a newline at the end of each
string, we can also make sure that the line we read ends with a newline
and otherwise wait for the rest of the line to be written.
The library we use in Go provides a convenient setting for this, while
in python and nodejs we need to add some code to do this ourselves.
Fixes https://github.com/pulumi/pulumi/issues/15235
Fixes https://github.com/pulumi/pulumi/issues/15652
Fixes https://github.com/pulumi/pulumi/issues/9269 (This is closed
already, but never had a proper resolution afaics)
Fixes https://github.com/pulumi/pulumi/issues/6768
It would be nice to add a typescript test here as well, but I'm not sure
how to do that without marking the readLines function non-private. But I
don't know typescript well, so any hints of how to do that would be
appreciated!
## Checklist
- [x] I have run `make tidy` to update any new dependencies
- [x] I have run `make lint` to verify my code passes the lint check
- [x] I have formatted my code using `gofumpt`
<!--- Please provide details if the checkbox below is to be left
unchecked. -->
- [x] I have added tests that prove my fix is effective or that my
feature works
<!---
User-facing changes require a CHANGELOG entry.
-->
- [x] I have run `make changelog` and committed the
`changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the
Pulumi Cloud,
then the service should honor older versions of the CLI where this
change would not exist.
You must then bump the API version in
/pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi
Cloud API version
<!-- @Pulumi employees: If yes, you must submit corresponding changes in
the service repo. -->
2024-03-26 14:32:56 +00:00
|
|
|
"time"
|
2022-04-22 16:50:40 +00:00
|
|
|
|
automation: only read complete lines before trying to deserialize (#15778)
When tailing the event log in automation API we currently have nothing
that makes sure we read only complete lines. This means if the OS
happens to flush an incomplete line for whatever reason (or the Go JSON
encoder does, which we're using to write these lines), we might read a
line that is incompletely written, and thus will fail to JSON decode it.
Since the JSON encoder always writes a newline at the end of each
string, we can also make sure that the line we read ends with a newline
and otherwise wait for the rest of the line to be written.
The library we use in Go provides a convenient setting for this, while
in python and nodejs we need to add some code to do this ourselves.
Fixes https://github.com/pulumi/pulumi/issues/15235
Fixes https://github.com/pulumi/pulumi/issues/15652
Fixes https://github.com/pulumi/pulumi/issues/9269 (This is closed
already, but never had a proper resolution afaics)
Fixes https://github.com/pulumi/pulumi/issues/6768
It would be nice to add a typescript test here as well, but I'm not sure
how to do that without marking the readLines function non-private. But I
don't know typescript well, so any hints of how to do that would be
appreciated!
## Checklist
- [x] I have run `make tidy` to update any new dependencies
- [x] I have run `make lint` to verify my code passes the lint check
- [x] I have formatted my code using `gofumpt`
<!--- Please provide details if the checkbox below is to be left
unchecked. -->
- [x] I have added tests that prove my fix is effective or that my
feature works
<!---
User-facing changes require a CHANGELOG entry.
-->
- [x] I have run `make changelog` and committed the
`changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the
Pulumi Cloud,
then the service should honor older versions of the CLI where this
change would not exist.
You must then bump the API version in
/pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi
Cloud API version
<!-- @Pulumi employees: If yes, you must submit corresponding changes in
the service repo. -->
2024-03-26 14:32:56 +00:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/auto/events"
|
2022-04-22 16:50:40 +00:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/auto/optpreview"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/auto/optup"
|
2024-02-29 21:06:24 +00:00
|
|
|
ptesting "github.com/pulumi/pulumi/sdk/v3/go/common/testing"
|
2022-04-22 16:50:40 +00:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-04-27 01:18:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const testPermalink = "Permalink: https://gotest"
|
|
|
|
|
|
|
|
func TestGetPermalink(t *testing.T) {
|
2022-03-04 08:17:41 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2021-04-27 01:18:45 +00:00
|
|
|
tests := map[string]struct {
|
|
|
|
testee string
|
|
|
|
want string
|
|
|
|
err error
|
|
|
|
}{
|
2023-12-12 12:19:42 +00:00
|
|
|
"successful parsing": {testee: testPermalink + "\n", want: "https://gotest"},
|
2021-04-27 01:18:45 +00:00
|
|
|
"failed parsing": {testee: testPermalink, err: ErrParsePermalinkFailed},
|
|
|
|
}
|
|
|
|
|
2022-03-04 08:17:41 +00:00
|
|
|
//nolint:paralleltest // false positive because range var isn't used directly in t.Run(name) arg
|
2021-04-27 01:18:45 +00:00
|
|
|
for name, test := range tests {
|
2022-03-04 08:17:41 +00:00
|
|
|
name, test := name, test
|
2021-04-27 01:18:45 +00:00
|
|
|
t.Run(name, func(t *testing.T) {
|
2022-03-04 08:17:41 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2021-04-27 01:18:45 +00:00
|
|
|
got, err := GetPermalink(test.testee)
|
|
|
|
if err != nil {
|
|
|
|
if test.err == nil || test.err != err {
|
|
|
|
t.Errorf("got '%v', want '%v'", err, test.err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if got != test.want {
|
|
|
|
t.Errorf("got '%s', want '%s'", got, test.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-04-22 16:50:40 +00:00
|
|
|
|
2022-06-01 19:25:20 +00:00
|
|
|
func TestUpdatePlans(t *testing.T) {
|
2022-04-22 16:50:40 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
2024-02-29 21:06:24 +00:00
|
|
|
sName := ptesting.RandomStackName()
|
2022-04-22 16:50:40 +00:00
|
|
|
stackName := FullyQualifiedStackName(pulumiOrg, pName, sName)
|
|
|
|
|
|
|
|
opts := []LocalWorkspaceOption{
|
|
|
|
SecretsProvider("passphrase"),
|
|
|
|
EnvVars(map[string]string{
|
|
|
|
"PULUMI_CONFIG_PASSPHRASE": "password",
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize
|
|
|
|
s, err := NewStackInlineSource(ctx, stackName, pName, func(ctx *pulumi.Context) error {
|
|
|
|
ctx.Export("exp_static", pulumi.String("foo"))
|
|
|
|
return nil
|
|
|
|
}, opts...)
|
|
|
|
require.NoError(t, err, "failed to initialize stack, err: %v", err)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
// -- pulumi stack rm --
|
|
|
|
err = s.Workspace().RemoveStack(ctx, s.Name())
|
|
|
|
assert.Nil(t, err, "failed to remove stack. Resources have leaked.")
|
|
|
|
}()
|
|
|
|
|
|
|
|
// first load settings for created stack
|
|
|
|
stackConfig, err := s.Workspace().StackSettings(ctx, stackName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
stackConfig.SecretsProvider = "passphrase"
|
|
|
|
assert.NoError(t, s.Workspace().SaveStackSettings(ctx, stackName, stackConfig))
|
|
|
|
|
|
|
|
// -- pulumi preview --
|
|
|
|
tempFile, err := os.CreateTemp("", "update_plan.json")
|
|
|
|
defer os.Remove(tempFile.Name())
|
|
|
|
|
|
|
|
_, err = s.Preview(ctx, optpreview.Plan(tempFile.Name()))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("preview failed, err: %v", err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
stat, err := tempFile.Stat()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("state failed, err: %v", err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if stat.Size() == 0 {
|
|
|
|
t.Errorf("expected update plan size to be non-zero")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- pulumi up --
|
|
|
|
|
|
|
|
upResult, err := s.Up(ctx, optup.Plan(tempFile.Name()))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("up failed, err: %v", err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
assert.Equal(t, "update", upResult.Summary.Kind)
|
|
|
|
assert.Equal(t, "succeeded", upResult.Summary.Result)
|
|
|
|
|
|
|
|
// -- pulumi destroy --
|
|
|
|
|
|
|
|
dRes, err := s.Destroy(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("destroy failed, err: %v", err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
assert.Equal(t, "destroy", dRes.Summary.Kind)
|
|
|
|
assert.Equal(t, "succeeded", dRes.Summary.Result)
|
|
|
|
}
|
automation: only read complete lines before trying to deserialize (#15778)
When tailing the event log in automation API we currently have nothing
that makes sure we read only complete lines. This means if the OS
happens to flush an incomplete line for whatever reason (or the Go JSON
encoder does, which we're using to write these lines), we might read a
line that is incompletely written, and thus will fail to JSON decode it.
Since the JSON encoder always writes a newline at the end of each
string, we can also make sure that the line we read ends with a newline
and otherwise wait for the rest of the line to be written.
The library we use in Go provides a convenient setting for this, while
in python and nodejs we need to add some code to do this ourselves.
Fixes https://github.com/pulumi/pulumi/issues/15235
Fixes https://github.com/pulumi/pulumi/issues/15652
Fixes https://github.com/pulumi/pulumi/issues/9269 (This is closed
already, but never had a proper resolution afaics)
Fixes https://github.com/pulumi/pulumi/issues/6768
It would be nice to add a typescript test here as well, but I'm not sure
how to do that without marking the readLines function non-private. But I
don't know typescript well, so any hints of how to do that would be
appreciated!
## Checklist
- [x] I have run `make tidy` to update any new dependencies
- [x] I have run `make lint` to verify my code passes the lint check
- [x] I have formatted my code using `gofumpt`
<!--- Please provide details if the checkbox below is to be left
unchecked. -->
- [x] I have added tests that prove my fix is effective or that my
feature works
<!---
User-facing changes require a CHANGELOG entry.
-->
- [x] I have run `make changelog` and committed the
`changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the
Pulumi Cloud,
then the service should honor older versions of the CLI where this
change would not exist.
You must then bump the API version in
/pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi
Cloud API version
<!-- @Pulumi employees: If yes, you must submit corresponding changes in
the service repo. -->
2024-03-26 14:32:56 +00:00
|
|
|
|
|
|
|
func TestAlwaysReadsCompleteLine(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tmpDir := t.TempDir()
|
|
|
|
tmpFile := tmpDir + "/test.txt"
|
|
|
|
go func() {
|
|
|
|
f, err := os.Create(tmpFile)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer f.Close()
|
|
|
|
parts := []string{
|
|
|
|
`{"stdoutEvent": `,
|
|
|
|
` {"message": "hello", "color": "blue"}}` + "\n",
|
|
|
|
`{"stdoutEvent": {"message":`,
|
|
|
|
` "world", "color": "red"}}` + "\n",
|
|
|
|
}
|
|
|
|
for _, part := range parts {
|
|
|
|
_, err = f.WriteString(part)
|
|
|
|
require.NoError(t, err)
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
engineEvents := make(chan events.EngineEvent, 20)
|
|
|
|
watcher, err := watchFile(tmpFile, []chan<- events.EngineEvent{engineEvents})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer watcher.Close()
|
|
|
|
event1 := <-engineEvents
|
|
|
|
require.NoError(t, event1.Error)
|
|
|
|
assert.Equal(t, "hello", event1.StdoutEvent.Message)
|
|
|
|
assert.Equal(t, "blue", event1.StdoutEvent.Color)
|
|
|
|
event2 := <-engineEvents
|
|
|
|
require.NoError(t, event2.Error)
|
|
|
|
assert.Equal(t, "world", event2.StdoutEvent.Message)
|
|
|
|
assert.Equal(t, "red", event2.StdoutEvent.Color)
|
|
|
|
}
|