-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from azaurus1/hhe-templating-configs
Hhe templating configs
- Loading branch information
Showing
6 changed files
with
177 additions
and
273 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,29 @@ | ||
name: unit-tests | ||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
paths: | ||
- '**' | ||
|
||
jobs: | ||
run: | ||
runs-on: ubuntu-latest | ||
environment: publish | ||
permissions: | ||
id-token: write | ||
contents: read | ||
steps: | ||
|
||
- uses: actions/[email protected] | ||
|
||
- uses: actions/setup-go@v5 | ||
with: | ||
go-version: '1.22' | ||
cache-dependency-path: ./ | ||
|
||
- name: Run Unit Tests for Templater | ||
working-directory: config-templating | ||
run: | | ||
go test -v |
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,41 @@ | ||
package config_templating | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"text/template" | ||
) | ||
|
||
type TableConfigTemplateParameters struct { | ||
KafkaBrokers string | ||
KafkaTopic string | ||
SchemaRegistryUrl string | ||
} | ||
|
||
func TemplateTableConfig(inputTemplate, outputConfigFile string, params TableConfigTemplateParameters) error { | ||
|
||
tableConfigTpl, err := os.ReadFile(inputTemplate) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
tableConfigTemplate, err := template.New(outputConfigFile).Parse(string(tableConfigTpl)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
outputFile, err := os.Create(outputConfigFile) | ||
if err != nil { | ||
return fmt.Errorf("error creating output file %s: %s", outputConfigFile, err) | ||
} | ||
|
||
defer outputFile.Close() | ||
|
||
err = tableConfigTemplate.Execute(outputFile, params) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
|
||
} |
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,65 @@ | ||
package config_templating | ||
|
||
import ( | ||
"github.com/azaurus1/go-pinot-api/model" | ||
"github.com/stretchr/testify/assert" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestTemplater(t *testing.T) { | ||
|
||
t.Run("Params correctly injected into template", func(t *testing.T) { | ||
|
||
// create temp file | ||
f, err := os.CreateTemp("", "test") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
defer os.Remove(f.Name()) | ||
|
||
// write to it | ||
config := dummyTableConfig() | ||
configBytes, err := config.AsBytes() | ||
assert.NoError(t, err) | ||
|
||
_, err = f.Write(configBytes) | ||
assert.NoError(t, err) | ||
|
||
// call TemplateTableConfig | ||
params := TableConfigTemplateParameters{ | ||
KafkaBrokers: "localhost:9092", | ||
KafkaTopic: "test", | ||
SchemaRegistryUrl: "http://localhost:8081", | ||
} | ||
|
||
err = TemplateTableConfig(f.Name(), f.Name(), params) | ||
assert.NoError(t, err) | ||
|
||
// read from it | ||
templatedConfig, err := model.NewTableConfigFromFile(f.Name()) | ||
assert.NoError(t, err) | ||
|
||
// assert that the template was correctly injected | ||
assert.Equal(t, "localhost:9092", templatedConfig.TableIndexConfig.StreamConfigs["stream.kafka.broker.list"]) | ||
assert.Equal(t, "test", templatedConfig.TableIndexConfig.StreamConfigs["stream.kafka.topic.name"]) | ||
assert.Equal(t, "http://localhost:8081", templatedConfig.TableIndexConfig.StreamConfigs["stream.kafka.decoder.prop.schema.registry.rest.url"]) | ||
|
||
}) | ||
|
||
} | ||
|
||
func dummyTableConfig() model.TableConfig { | ||
return model.TableConfig{ | ||
TableName: "dummy", | ||
TableType: "REALTIME", | ||
TableIndexConfig: model.TableIndexConfig{ | ||
StreamConfigs: map[string]string{ | ||
"stream.kafka.topic.name": "{{ .KafkaTopic }}", | ||
"stream.kafka.broker.list": "{{ .KafkaBrokers }}", | ||
"stream.kafka.decoder.prop.schema.registry.rest.url": "{{ .SchemaRegistryUrl }}", | ||
}, | ||
}, | ||
} | ||
} |
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
Oops, something went wrong.