Skip to content

Commit

Permalink
Move configschema from collector core to collector contrib (#5038)
Browse files Browse the repository at this point in the history
* Move cmd configschema

* Fix unit test

* Update configschema paths

* Remove unnecessary replace statement

* fixup test assumptions

Signed-off-by: Anthony J Mirabella <[email protected]>

* mod tidy

Signed-off-by: Anthony J Mirabella <[email protected]>

* fix configschema tests, some docsgen tests

Signed-off-by: Anthony J Mirabella <[email protected]>

* Fix remaining docsgen tests

Signed-off-by: Anthony J Mirabella <[email protected]>

* mod tidy

Signed-off-by: Anthony J Mirabella <[email protected]>

* Update collector, run lint with -j2

Signed-off-by: Bogdan Drutu <[email protected]>

Co-authored-by: alex <[email protected]>
Co-authored-by: Anthony J Mirabella <[email protected]>
  • Loading branch information
3 people authored Aug 31, 2021
1 parent 827de29 commit f4e9607
Show file tree
Hide file tree
Showing 23 changed files with 3,584 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ jobs:
path: /home/runner/go/bin
key: v1-tools-${{ runner.os }}-${{ hashFiles('./internal/tools/go.mod') }}
- name: Lint
run: make -j4 golint
run: make -j2 golint
- name: Checks
run: make -j4 checklicense misspell checkdoc
- name: Codegem
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

func TestFieldComments(t *testing.T) {
v := reflect.ValueOf(testStruct{})
comments, err := commentsForStruct(v, testDR())
comments, err := commentsForStruct(v, testDR("../.."))
assert.NoError(t, err)
assert.Equal(t, "embedded, package qualified comment\n", comments["Duration"])
assert.Equal(t, "testStruct comment\n", comments["_struct"])
Expand All @@ -34,7 +34,7 @@ func TestExternalType(t *testing.T) {
u, err := uuid.NewUUID()
assert.NoError(t, err)
v := reflect.ValueOf(u)
comments, err := commentsForStruct(v, testDR())
comments, err := commentsForStruct(v, testDR("."))
assert.NoError(t, err)
assert.Equal(t, "A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC\n4122.\n", comments["_struct"])
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ type testStruct struct {
Ignored string `mapstructure:"-"`
}

func testDR() DirResolver {
func testDR(root string) DirResolver {
return DirResolver{
SrcRoot: "../..",
SrcRoot: root,
ModuleName: DefaultModule,
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
"testing"

"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/service/defaultcomponents"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/components"
)

func TestGetAllConfigs(t *testing.T) {
Expand Down Expand Up @@ -72,9 +72,9 @@ func TestGetConfig(t *testing.T) {
}

func testComponents() component.Factories {
components, err := defaultcomponents.Components()
cmps, err := components.Components()
if err != nil {
panic(err)
}
return components
return cmps
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ import (
"strings"
"text/template"

"go.opentelemetry.io/collector/cmd/configschema"
"go.opentelemetry.io/collector/component"

"github.com/open-telemetry/opentelemetry-collector-contrib/cmd/configschema"
)

const mdFileName = "config.md"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/cmd/configschema"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/receiver/otlpreceiver"
"go.opentelemetry.io/collector/service/defaultcomponents"

"github.com/open-telemetry/opentelemetry-collector-contrib/cmd/configschema"
)

func TestWriteConfigDoc(t *testing.T) {
Expand All @@ -44,8 +44,8 @@ func TestWriteConfigDoc(t *testing.T) {
outputFilename = dir
return nil
})
expectedPath := path.Join(root, "receiver/otlpreceiver/config.md")
assert.Equal(t, expectedPath, outputFilename)
expectedPath := "receiver/otlpreceiver/config.md"
assert.True(t, strings.HasSuffix(outputFilename, expectedPath))
}

func testTemplate(t *testing.T) *template.Template {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"strings"
"text/template"

"go.opentelemetry.io/collector/cmd/configschema"
"github.com/open-telemetry/opentelemetry-collector-contrib/cmd/configschema"
)

func renderHeader(typ, group, doc string) []byte {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/cmd/configschema"
"github.com/open-telemetry/opentelemetry-collector-contrib/cmd/configschema"
)

func TestTableTemplate(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
package main

import (
"go.opentelemetry.io/collector/cmd/configschema"
"go.opentelemetry.io/collector/cmd/configschema/docsgen/docsgen"
"go.opentelemetry.io/collector/service/defaultcomponents"

"github.com/open-telemetry/opentelemetry-collector-contrib/cmd/configschema"
"github.com/open-telemetry/opentelemetry-collector-contrib/cmd/configschema/docsgen/docsgen"
)

func main() {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func getField(fields []*Field, name string) *Field {
func testReadFields(t *testing.T, s testStruct, defaults map[string]interface{}) {
root, _ := ReadFields(
reflect.ValueOf(s),
testDR(),
testDR("../.."),
)

assert.Equal(t, "testStruct comment\n", root.Doc)
Expand Down
614 changes: 614 additions & 0 deletions cmd/configschema/go.mod

Large diffs are not rendered by default.

2,933 changes: 2,933 additions & 0 deletions cmd/configschema/go.sum

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const DefaultSrcRoot = "."

// DefaultModule is the module prefix of otelcol. Can be used to create a
// DirResolver.
const DefaultModule = "go.opentelemetry.io/collector"
const DefaultModule = "github.com/open-telemetry/opentelemetry-collector-contrib"

// DirResolver is used to resolve the base directory of a given reflect.Type.
type DirResolver struct {
Expand Down Expand Up @@ -109,15 +109,15 @@ func grepMod(goModPath string, pkg string) (pkgPath string, version string, err
switch {
case line.Old.Path == DefaultModule:
pkgPath, version = line.New.Path, line.New.Version
case strings.Contains(line.Old.Path, pkg):
case strings.HasPrefix(pkg, line.Old.Path):
return line.New.Path, line.New.Version, nil
}
}
for _, line := range modContents.Require {
switch {
case pkgPath == "" && line.Mod.Path == DefaultModule:
pkgPath, version = line.Mod.Path, line.Mod.Version
case strings.Contains(line.Mod.Path, pkg):
case strings.HasPrefix(pkg, line.Mod.Path):
return line.Mod.Path, line.Mod.Version, nil
}
}
Expand All @@ -137,6 +137,6 @@ func (dr DirResolver) buildExternalPath(goPath, pkg, line, v string) string {
case strings.HasPrefix(line, "../"):
return path.Join(dr.SrcRoot, line, strings.TrimPrefix(pkg, DefaultModule))
default:
return path.Join(goPath, "pkg", "mod", line+"@"+v)
return path.Join(goPath, "pkg", "mod", line+"@"+v, strings.TrimPrefix(pkg, line))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/collector/model/pdata"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/resourcetotelemetry"
)

func TestPackageDirLocal(t *testing.T) {
pkg := pdata.NewSum()
pkg := resourcetotelemetry.Settings{}
pkgValue := reflect.ValueOf(pkg)
dr := testDR()
dr := testDR("../..")
output, err := dr.PackageDir(pkgValue.Type())
assert.NoError(t, err)
assert.Equal(t, "../../model/pdata", output)
assert.Equal(t, "../../pkg/resourcetotelemetry", output)
}

func TestPackageDirError(t *testing.T) {
Expand All @@ -48,7 +49,7 @@ func TestPackageDirError(t *testing.T) {

func TestExternalPkgDirErr(t *testing.T) {
pkg := "random/test"
pkgPath, err := testDR().externalPackageDir(pkg)
pkgPath, err := testDR("../..").externalPackageDir(pkg)
if assert.Error(t, err) {
expected := fmt.Sprintf("could not find package: \"%s\"", pkg)
assert.EqualErrorf(t, err, expected, "")
Expand All @@ -57,8 +58,8 @@ func TestExternalPkgDirErr(t *testing.T) {
}

func TestExternalPkgDir(t *testing.T) {
dr := testDR()
testPkg := "grpc-ecosystem/grpc-gateway"
dr := testDR("../..")
testPkg := "github.com/grpc-ecosystem/grpc-gateway/runtime"
pkgPath, err := dr.externalPackageDir(testPkg)
assert.NoError(t, err)
goPath := os.Getenv("GOPATH")
Expand All @@ -67,13 +68,13 @@ func TestExternalPkgDir(t *testing.T) {
}
testLine, testVers, err := grepMod(path.Join(dr.SrcRoot, "go.mod"), testPkg)
assert.NoError(t, err)
expected := fmt.Sprint(path.Join(goPath, "pkg", "mod", testLine+"@"+testVers))
expected := fmt.Sprint(path.Join(goPath, "pkg", "mod", testLine+"@"+testVers, "runtime"))
assert.Equal(t, expected, pkgPath)
}

func TestExternalPkgDirReplace(t *testing.T) {
pkg := DefaultModule + "/model"
pkgPath, err := testDR().externalPackageDir(pkg)
pkg := DefaultModule + "/pkg/resourcetotelemetry"
pkgPath, err := testDR("../..").externalPackageDir(pkg)
assert.NoError(t, err)
assert.Equal(t, "../../model", pkgPath)
assert.Equal(t, "../../pkg/resourcetotelemetry", pkgPath)
}

0 comments on commit f4e9607

Please sign in to comment.