2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// 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
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// 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.
|
|
|
|
|
2017-03-28 08:53:06 +00:00
|
|
|
package datastore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-08-25 06:39:19 +00:00
|
|
|
"io"
|
2017-03-28 08:53:06 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2021-09-27 17:51:55 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2017-03-28 08:53:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFileFind(t *testing.T) {
|
2021-11-13 19:18:06 +00:00
|
|
|
store, closer := newTestStore(t, new(model.File), new(model.Proc))
|
|
|
|
defer closer()
|
2017-03-28 08:53:06 +00:00
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
if err := store.FileCreate(
|
2017-03-28 08:53:06 +00:00
|
|
|
&model.File{
|
2022-10-18 01:24:12 +00:00
|
|
|
PipelineID: 2,
|
|
|
|
ProcID: 1,
|
|
|
|
Name: "hello.txt",
|
|
|
|
Mime: "text/plain",
|
|
|
|
Size: 11,
|
2017-03-28 08:53:06 +00:00
|
|
|
},
|
|
|
|
bytes.NewBufferString("hello world"),
|
|
|
|
); err != nil {
|
|
|
|
t.Errorf("Unexpected error: insert file: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
file, err := store.FileFind(&model.Proc{ID: 1}, "hello.txt")
|
2017-03-28 08:53:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got, want := file.ID, int64(1); got != want {
|
|
|
|
t.Errorf("Want file id %d, got %d", want, got)
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
if got, want := file.PipelineID, int64(2); got != want {
|
|
|
|
t.Errorf("Want file pipeline id %d, got %d", want, got)
|
2017-03-28 08:53:06 +00:00
|
|
|
}
|
|
|
|
if got, want := file.ProcID, int64(1); got != want {
|
|
|
|
t.Errorf("Want file proc id %d, got %d", want, got)
|
|
|
|
}
|
|
|
|
if got, want := file.Name, "hello.txt"; got != want {
|
|
|
|
t.Errorf("Want file name %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
if got, want := file.Mime, "text/plain"; got != want {
|
|
|
|
t.Errorf("Want file mime %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
if got, want := file.Size, 11; got != want {
|
|
|
|
t.Errorf("Want file size %d, got %d", want, got)
|
|
|
|
}
|
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
rc, err := store.FileRead(&model.Proc{ID: 1}, "hello.txt")
|
2017-03-28 08:53:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2022-08-25 06:39:19 +00:00
|
|
|
out, _ := io.ReadAll(rc)
|
2017-03-28 08:53:06 +00:00
|
|
|
if got, want := string(out), "hello world"; got != want {
|
|
|
|
t.Errorf("Want file data %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileList(t *testing.T) {
|
2022-10-18 01:24:12 +00:00
|
|
|
store, closer := newTestStore(t, new(model.File), new(model.Pipeline))
|
2021-11-13 19:18:06 +00:00
|
|
|
defer closer()
|
2017-03-28 08:53:06 +00:00
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
assert.NoError(t, store.FileCreate(
|
2017-03-28 08:53:06 +00:00
|
|
|
&model.File{
|
2022-10-18 01:24:12 +00:00
|
|
|
PipelineID: 1,
|
|
|
|
ProcID: 1,
|
|
|
|
Name: "hello.txt",
|
|
|
|
Mime: "text/plain",
|
|
|
|
Size: 11,
|
2017-03-28 08:53:06 +00:00
|
|
|
},
|
|
|
|
bytes.NewBufferString("hello world"),
|
2021-11-13 19:18:06 +00:00
|
|
|
))
|
|
|
|
assert.NoError(t, store.FileCreate(
|
2017-03-28 08:53:06 +00:00
|
|
|
&model.File{
|
2022-10-18 01:24:12 +00:00
|
|
|
PipelineID: 1,
|
|
|
|
ProcID: 1,
|
|
|
|
Name: "hola.txt",
|
|
|
|
Mime: "text/plain",
|
|
|
|
Size: 11,
|
2017-03-28 08:53:06 +00:00
|
|
|
},
|
|
|
|
bytes.NewBufferString("hola mundo"),
|
2021-11-13 19:18:06 +00:00
|
|
|
))
|
2017-03-28 08:53:06 +00:00
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
files, err := store.FileList(&model.Pipeline{ID: 1})
|
2017-03-28 08:53:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: select files: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := len(files), 2; got != want {
|
|
|
|
t.Errorf("Wanted %d files, got %d", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileIndexes(t *testing.T) {
|
2022-10-18 01:24:12 +00:00
|
|
|
store, closer := newTestStore(t, new(model.File), new(model.Pipeline))
|
2021-11-13 19:18:06 +00:00
|
|
|
defer closer()
|
2017-03-28 08:53:06 +00:00
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
if err := store.FileCreate(
|
2017-03-28 08:53:06 +00:00
|
|
|
&model.File{
|
2022-10-18 01:24:12 +00:00
|
|
|
PipelineID: 1,
|
|
|
|
ProcID: 1,
|
|
|
|
Name: "hello.txt",
|
|
|
|
Size: 11,
|
|
|
|
Mime: "text/plain",
|
2017-03-28 08:53:06 +00:00
|
|
|
},
|
|
|
|
bytes.NewBufferString("hello world"),
|
|
|
|
); err != nil {
|
|
|
|
t.Errorf("Unexpected error: insert file: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// fail due to duplicate file name
|
2021-11-13 19:18:06 +00:00
|
|
|
if err := store.FileCreate(
|
2017-03-28 08:53:06 +00:00
|
|
|
&model.File{
|
2022-10-18 01:24:12 +00:00
|
|
|
PipelineID: 1,
|
|
|
|
ProcID: 1,
|
|
|
|
Name: "hello.txt",
|
|
|
|
Mime: "text/plain",
|
|
|
|
Size: 11,
|
2017-03-28 08:53:06 +00:00
|
|
|
},
|
|
|
|
bytes.NewBufferString("hello world"),
|
|
|
|
); err == nil {
|
2021-10-08 16:35:56 +00:00
|
|
|
t.Errorf("Unexpected error: duplicate pid")
|
2017-03-28 08:53:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
func TestFileCascade(t *testing.T) {
|
2022-10-18 01:24:12 +00:00
|
|
|
store, closer := newTestStore(t, new(model.File), new(model.Proc), new(model.Pipeline))
|
2021-11-13 19:18:06 +00:00
|
|
|
defer closer()
|
|
|
|
|
|
|
|
procOne := &model.Proc{
|
2022-10-18 01:24:12 +00:00
|
|
|
PipelineID: 1,
|
|
|
|
PID: 1,
|
|
|
|
PGID: 1,
|
|
|
|
Name: "build",
|
|
|
|
State: "success",
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
err1 := store.ProcCreate([]*model.Proc{procOne})
|
|
|
|
assert.EqualValues(t, int64(1), procOne.ID)
|
|
|
|
|
|
|
|
err2 := store.FileCreate(
|
|
|
|
&model.File{
|
2022-10-18 01:24:12 +00:00
|
|
|
PipelineID: 1,
|
|
|
|
ProcID: 1,
|
|
|
|
Name: "hello.txt",
|
|
|
|
Mime: "text/plain",
|
|
|
|
Size: 11,
|
2021-11-13 19:18:06 +00:00
|
|
|
},
|
|
|
|
bytes.NewBufferString("hello world"),
|
|
|
|
)
|
|
|
|
|
|
|
|
if err1 != nil {
|
|
|
|
t.Errorf("Unexpected error: cannot insert proc: %s", err1)
|
|
|
|
} else if err2 != nil {
|
|
|
|
t.Errorf("Unexpected error: cannot insert file: %s", err2)
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
if _, err3 := store.ProcFind(&model.Pipeline{ID: 1}, 1); err3 != nil {
|
2021-11-13 19:18:06 +00:00
|
|
|
t.Errorf("Unexpected error: cannot get inserted proc: %s", err3)
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
err := store.ProcClear(&model.Pipeline{ID: 1, Procs: []*model.Proc{procOne}})
|
2021-11-13 19:18:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
file, err4 := store.FileFind(&model.Proc{ID: 1}, "hello.txt")
|
|
|
|
if err4 == nil {
|
|
|
|
t.Errorf("Expected no rows in result set error")
|
|
|
|
t.Log(file)
|
|
|
|
}
|
|
|
|
}
|